5

I'm using iterable Differs from Angular2 to detect changes on my data. Then I want to reload my view. But the differ.diff always returns "null" and I don't know why.

constructor(differs: IterableDiffers) {
    this.differ = differs.find([]).create(null);
}

@Input() data: any;

ngDoCheck() {
    var changes = this.differ.diff(this.data.datasets);
    if (changes && this.initialized) {
       //doreload
    }

EDIT:

this.pingService.pingStream.subscribe(ping => {
                  this.ping = ping;
                  console.log(this.ping);
                  NTWDATA.datasets[0].data.shift();
                  NTWDATA.datasets[0].data.push(this.ping);

PingService returns a number every 5secs(pinging my server). shift/push thingy works fine, data is there. It just doesn't get detected. NTWDATA:

{
    labels: ["","","","","","",""],
    datasets: [
        {
            label: 'Server Response Time in ms',
            data: [65, 59, 80, 81, 56, 55, 40],
            fill: false,
            borderColor: '#FF0303'
        }
    ]
}
johnny 5
  • 19,893
  • 50
  • 121
  • 195
Faigjaz
  • 818
  • 3
  • 15
  • 30

1 Answers1

6

It depends on what you want to detect. With the IterableDiffers class, you will "only" detect if:

  • elements are added in your array
  • elements are removed from your array

But it won't detect if updates are done within elements of your array.

This question could interest you:

See this plunkr for the use case: https://plnkr.co/edit/wn6mTEcvrW2vh1ko5Ji5?p=preview.

Community
  • 1
  • 1
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • First I use shift to remove first element, then I do a push. So actually both cases. – Faigjaz Jul 20 '16 at 08:03
  • 1
    I made a test with your use case and it seems to work. See this plunkr: https://plnkr.co/edit/wn6mTEcvrW2vh1ko5Ji5?p=preview. – Thierry Templier Jul 20 '16 at 08:11
  • I see. In my case I'm changing the datasets[0].data. When I set the changes to "changes= this.differ.diff(this.data.datasets[0].data)" my changes get detected and it works. But this is only a workaround as it won't work for other datasets within my NTWDATA. Any ideas on how to make it more "general"? – Faigjaz Jul 20 '16 at 08:35
  • The problem is that differs only support one level and not the complete data graph. For recursive support, you need to implement it by your own... – Thierry Templier Jul 20 '16 at 09:57
  • @Thierry Templier Seems that IterableDiffers, beside the two situations you mentioned in the answer, also detects the element's instance change. – seidme Feb 24 '17 at 11:15