i see that you are confused between two lifecycle hooks.
In Angular , we can perform change detection using two different lifecycle hooks :
the ngOnchanges lifecycle hook and the ngDoCheck lifecycle hook.
But in some cases, the ngOnChanges is not enough to detect all changes because it uses the property reference to check the difference, so it can’t detect the changes if you update the object property.
Meanwhile, the ngDoCheck lifecycle hook allows us to perform our custom change detection so we can bypass the ngOnChange lifecycle hook limitations.
For example, if you pass a User Object from the parent component then you update the age or the username property of the User object, in this case the Angular will not fire the event to dispatch the ngOnChange callback. Because it has the same reference of the User object.
So you can resolve this problem by using ngDoCheck lifecycle hook.
by using ngDoCheck , we can use more advanced tools like key-value differs and iterable differs services to perform custom change detections.
check this little example :
//child-component.ts
import {Component, DoCheck, Input, KeyValueDiffers, OnInit} from "@angular/core";
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
})
export class ChildComponent implements OnInit, DoCheck {
@Input() user: any;
changelogList: Array<string> = [];
differ: any;
constructor(private differs: KeyValueDiffers) {
}
ngOnInit() {
this.differ = this.differs.find(this.user).create();
}
ngDoCheck() {
const userChanges = this.differ.diff(this.user);
if (userChanges) {
userChanges.forEachChangedItem((changeRecord: any) => {
this.changelogList.push('item changed : ' + changeRecord.key + ' ' + JSON.stringify(changeRecord.currentValue))
});
userChanges.forEachAddedItem((changeRecord: any) => {
this.changelogList.push('item added : ' + changeRecord.key + ' ' + JSON.stringify(changeRecord.currentValue))
});
}
}
}
the differ method inside the ngDoCheck returns un object that provides multiples advanced methods such as
forEachItem, forEachPreviousItem, forEachChangedItem, forEachAddedItem, forEachRemovedItem.