I can\'t seem to get angular2 view to be updated on an array.push function, called upon from a setInterval async operation.
the code is from this angular plunkr example
You need to update the whole reference of your array after adding an element in it:
constructor(private ref: ChangeDetectorRef) {
setInterval(() => {
this.numberOfTicks.push(3);
this.numberOfTicks = this.numberOfTicks.slice();
this.ref.markForCheck();
}, 1000);
}
}
Edit
The DoCheck interface could also interest you since it allows you to plug your own change detection algorithm.
See this link for more details:
Here is a sample:
@Component({
selector: 'custom-check',
template: `
- {{line}}
`
})
class CustomCheckComponent implements DoCheck {
@Input() list: any[];
differ: any;
logs = [];
constructor(differs: IterableDiffers) {
this.differ = differs.find([]).create(null);
}
ngDoCheck() {
var changes = this.differ.diff(this.list);
if (changes) {
changes.forEachAddedItem(r => this.logs.push('added ' + r.item));
changes.forEachRemovedItem(r => this.logs.push('removed ' + r.item))
}
}
}