0

I have detected a bug in Angular 4 or PrimNG Datatables module.

I have a simple view:

<span *ngFor="let node of nodes">{{node.label}}</span> //works with case 1,2,3 and 4

<p-dataTable [value]="nodes"> //works with case 1, 2, and 3 (but not with 4!)
     <p-column field="label" header="Label"></p-column>
</p-dataTable>

and code in component:

private nodes:any[] = []

ngOnChanges() {
   //case 1 -> ok
   //this.nodes.push({label: 'foo'})

   //case 2 -> ok
   //this.nodes = [{label: 'foo'}]

   this.someService.getAll().subscribe(                     
        records => {  
             //case 3 ->ok
             //this.nodes = [{label: 'foo'}]

             //case 4 -> BUG
             //this.nodes.push({label: 'foo'}) //datatable is not updated
        }
   )
}

When I run this code with uncommented case 1, 2 or 3 everything is OK but when I am trying to run case 4 (same as case 1 but after service resolve) - prim ng datatables seem not to see changes. In Angular 2 I did not have such issue. Could you explain me what is going on here? :)

Regards

Michal Bialek
  • 499
  • 1
  • 10
  • 26

1 Answers1

0

You should have the same behaviour in angular2 and angular4. In fact it's not a bug, it's the expected behaviour. It as to do with how angular detect change. To detect change angular just do a ===.

In your case 1 it works because the changeDetection is triggered by the ngOnChange itself.

But in your case 4 you just add a value to your array without changing the reference of the array nodes.

For the other cases you change the reference to nodes so angular is able to detect changes.

I advise you to look a this answer it has a lot more details: Angular2 change detection: ngOnChanges not firing for nested object

Community
  • 1
  • 1
runit
  • 376
  • 1
  • 8
  • So, why *ngFor detects changes properly but primeng not? – Michal Bialek May 17 '17 at 12:42
  • ho ! I didn't see the comment in the html ... my bad. I tried it and for me it's not working with ngFor... You launched your case 1 by 1 right ? – runit May 17 '17 at 13:14
  • how it is possible. Look at any tutorial in internet, when you push something to array it is imiedietelly seen in view when you use *ngFor. Here is the same. – Michal Bialek May 17 '17 at 14:03