Categories

Jan 15 in Angular

Q: What are the lifecycle hooks for components and directives?

1 Answer

Jan 15

An Angular component has a discrete life-cycle which contains different phases as it transits through birth till death. In order to gain better control of these phases, we can hook into them using the following:

constructor: It is invoked when a component or directive is created by calling new on the class.

ngOnChanges: It is invoked whenever there is a change or update in any of the input properties of the component.

ngOnInit: It is invoked every time a given component is initialized. This hook is only once called in its lifetime after the first ngOnChanges.

ngDoCheck: It is invoked whenever the change detector of the given component is called. This allows you to implement your own change detection algorithm for the provided component.

ngOnDestroy: It is invoked right before the component is destroyed by Angular. You can use this hook in order to unsubscribe observables and detach event handlers for avoiding any kind of memory leaks.

Click here to read more about Angular JS
Click here to read more about Insurance

Related questions

Madanswer
Nov 24, 2019 in Angular
Sep 13, 2019 in Angular
Sep 13, 2019 in Angular
...