In Angular 2+, ngDoCheck and ngAfterViewChecked seems to perform same function.
ngDoCheck
is said to be called whenever change detection is triggered. Now this change detection will be triggered with change in View. As per docs, ngAfterViewChecked
is called whenever view changes.
What is the need of two lifecycle hooks here when one was sufficient?
There is a huge difference:
ngDoCheck is called very often, on each change detection run, and you should normally avoid to use it to avoid performance problems. It will detect the changes on any element, content or view change behavior.
ngAfterViewChecked is only called after the bindings of the view children are checked (it is related to the view only).