Site icon NgDeveloper

Angular 9 Lifecycle Hooks for 2Mins Read

Angular 5 Lifecycle Hooks for 2Mins Read:

What are lifecycle hooks ?
Lifecycle hooks are the different phases during component initializing and projection to view, when a new component is encountered in the selector, then angular takes care of initializing the component to project it in the view with different phases. These are called lifecycle hooks in angular.

All these lifecycle hooks are called after the component’s constructor called.

 

These are lifecycle hooks of angular:

ngOnChanges():

ngOnInit():

ngDoCheck():

ngAfterContentInit():

Called after the content has been initialized and projected to view, but still it is in typescript component level only and not displayed in the view still.

 

ngAfterContentChecked():

Called every time the content has been projected + doCheck completed.

In realtime, I was facing the issue like when the routing url changed, the content alone was not getting refreshed when I do from the same page.

So I copied all those url value reading from ngOnInit() to ngAfterContentChecked() to refresh the view as well.

 

ngAfterViewInit():

 

ngAfterViewChecked():

Called everytime the view has been projected + doCheck completed

 

ngOnDestroy():

 

 

Things to Remember / Note:

 

Angular lifecycle hooks are little tricky to understand if you are completely new to angular. But you should know it in highlevel atleast to be a better angular programmer.

If someone will be able to explain much better than this article, please add your content/examples in the below comments area to update and help angular new developers.

 

Exit mobile version