I have a component which receives an input which is an object. onchanges
does not detect changes on an object. So I need to use DoCheck to watch for changes on input object. So, in this case, I cannot use oninit
method because even first-time doCheck
method is hit. The problem is I cannot know is it the first time the data is initialized or its just data change. I need to do just some modification on data changes. Is there a better way to detect changes on input when it's an object so that I can know when it's the first set of data or just change of data.
Asked
Active
Viewed 413 times
0
-
Add an observable to the object that emits an event when a property is changes. You can do this by making properties getters/setters and emit an event in setters. – Günter Zöchbauer Jul 19 '17 at 06:15
-
`ngOnInit` is called only once so you will know this the data initializations. Then inside `ngDoCheck` you can check for changes – Max Koretskyi Jul 19 '17 at 06:48
-
Yes. First time both get called ngoninit and ngdocheck also. – Hacker Jul 19 '17 at 07:13