Currently using Angular 2 and typescript, I have an array in which I am utilizing DoCheck and IterableDiffer to monitor any changes. While I receive notifications when the array itself is modified, I do not get notified if a property within one of the objects in the array changes. I attempted using KeyValueDiffer without success. It seems like I might need to adjust how I am using "_differs.find". Any suggestions?
@Component({
selector: 'test'
})
@View({
template: `
<div>hello!</div>`
})
export class MyComponent implements DoCheck {
private _differ: IterableDiffer;
private _differ2: KeyValueDiffer;
_myItems: MyItem[];
@Input()
set myItems(value: MyItem[]) {
this._myItems = value;
if (!this._differ && value) {
this._differ = this._iterableDiffers.find([]).create(null);
}
if (!this._differ2 && value) {
this._differ2 = this._differs.find(this._myItems).create(null);
}
}
get myItems() {
return this._myItems;
}
constructor(private _iterableDiffers: IterableDiffers, private _differs: KeyValueDiffers, private _myService: MyService) {
this.myItems = _myService.getItems();
}
ngDoCheck() {
var changes = this._differ.diff(this._myItems);
if (changes) {
changes.forEachAddedItem((record) => {
console.log('added ' + record.item);
});
changes.forEachRemovedItem((record) => {
console.log('removed ' + record.item);
});
}
var changes2 = this._differ2.diff(this._myItems);
if (changes2) {
changes2.forEachChangedItem(
(record) => {
console.log(record.key + "," + record.currentValue);
}
});
}
}
}
It appears challenging to receive notifications for changes within a property of MyItem.