1

In previous angular version we had $scope.apply to detect changes , So i below code i have data from detailService that is printed now i am pushing data to object its throwing error object property is undefined , what is correct approach in new angular version to push data to array and bind it to the dom ?

app.component.ts

  import { Component, OnInit,Pipe, PipeTransform, EventEmitter, Output } from '@angular/core';
import { DetailService } from '../detail.service';
import { StreamService } from '../stream.service';
import { MatTableDataSource } from '@angular/material';
import {GtConfig} from '@angular-generic-table/core';
import { GenericTableComponent} from '@angular-generic-table/core';
import * as io from 'socket.io-client';


export interface Element {
        ticketNum: number;
        ticketOpened: number;
        eventType: string;
        riskIndex: string;
        riskValue: number;
        severity: string;
        lastModifiedDate: number;
        assetID: string;
    }
    @Component({
      selector: 'app-detail',
      templateUrl: './detail.component.html',
      styleUrls: ['./detail.component.css'],

    })
export class DetailComponent{
  messageArray: any[];
    message1:Object = {};
   public secondConfigObject: GtConfig<any>;

    constructor(private detailService: DetailService) {
        this.secondConfigObject = {
            settings: this.getBaseSettings(),
            fields: this.getBaseFields(),
            data: []
        };
    };

    ngOnInit() {
        this.detailService.currentMessage1.subscribe(message1 => {
            console.log('eventINDetailComp',message1);
            this.secondConfigObject.data.push(message1);
        });
    }

}

app.component.html

<div class="table-responsive">
    <generic-table [gtClasses]="'table-hover'" #myCustomTable [gtSettings]="secondConfigObject.settings" [gtFields]="secondConfigObject.fields" [gtData]="secondConfigObject.data"></generic-table>
</div>
13
  • Do you use ChangeDetectionStrategy.OnPush? If so, you can try removing that option. Otherwise, you will find a few ways to force change detection in this answer. Commented Jan 19, 2018 at 15:24
  • @ConnorsFan changes are automatically detected within angular 2+ Commented Jan 19, 2018 at 15:26
  • @mast3rd3mon - Not always, especially with ChangeDetectionStrategy.OnPush. Commented Jan 19, 2018 at 15:28
  • @ConnorsFan unless running something outside the angular scope, it always does, thats the benefits of using angular over angularjs or other frameworks Commented Jan 19, 2018 at 15:29
  • @mast3rd3mon - I invite you to take a look at the cases illustrated in this document. Commented Jan 19, 2018 at 15:56

1 Answer 1

2

You should move the code from the constructor to the start of the ngOnInit() function so the data gets set once the page has been created, not during.

As for data binding, variables on the screen/html will automatically update when they are changed in the code behind

Sign up to request clarification or add additional context in comments.

8 Comments

it throws error at message1.prop is undefined if i move code inside ngOnInit not sure why this is invoking , this is row click event it should only invoke when we have data pass from detail service
where do you use message1.prop? i cant see it in your code?
i am just giving you example so in subscribe i have message1 that is the object coming from service now properties are undefined on that because this.detailService.currentMessage1 invoked on application launch it should not
if you put the subscribe in the ngOnInit, then it should be invoked when the application is launched
but i am passing the event when row is clicked in the table what will be correct approach in that case, basically passing data object from one table to another
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.