in Angular by
What are the lifecycle hooks for components and directives?

► Click here to show 1 Answer

0 votes

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.

Angular JS
Learn More with Madanswer

Related questions

0 votes
asked Sep 13, 2019 in Angular by ivor2019