I\'d like to watch an object/array, which ca be edited by a service or by a controllers routine. I thought that an Observable could watch an object/array.
My implem
Angular2 provides IterableDiffer
(array) and KeyValueDiffer
(object) to get information about differences between two checks.
NgClass
is a good example https://github.com/angular/angular/blob/14ee75924b6ae770115f7f260d720efa8bfb576a/modules/%40angular/common/src/directives/ng_class.ts#L122
See also https://angular.io/docs/ts/latest/api/#!?query=differ
An example
// inject a differ implementation
constructor(differs: KeyValueDiffers) {
// store the initial value to compare with
this.differ = differs.find({}).create(null);
}
@Input() data: any;
ngDoCheck() {
var changes = this.differ.diff(this.data); // check for changes
if (changes && this.initialized) {
// do something if changes were found
}
}