What are lifecycle hooks in angular and how many lifecycle hooks are there in angular ?

Priti Jha
2 min readJan 23, 2023

--

Lifecycle hooks are a set of methods in Angular that are executed at specific moments during the lifecycle of a component. These methods provide a way to tap into the Angular component lifecycle and perform custom logic or operations at specific points in time.

There are eight lifecycle hooks in Angular:

  1. ngOnChanges: This lifecycle hook is executed whenever the component's input properties change.
  2. ngOnInit: This lifecycle hook is executed after the component's constructor method and is a good place to perform initial setup for the component.
  3. ngDoCheck: This lifecycle hook is executed during every change detection cycle and is a good place to perform custom change detection.
  4. ngAfterContentInit: This lifecycle hook is executed after the component's content has been initialized and is a good place to perform additional setup for the component's content.
  5. ngAfterContentChecked: This lifecycle hook is executed after the component's content has been checked and is a good place to perform additional operations based on the component's content.
  6. ngAfterViewInit: This lifecycle hook is executed after the component's view has been initialized and is a good place to perform additional setup for the component's view.
  7. ngAfterViewChecked: This lifecycle hook is executed after the component's view has been checked and is a good place to perform additional operations based on the component's view.
  8. ngOnDestroy: This lifecycle hook is executed just before the component is destroyed and is a good place to perform cleanup operations for the component.

Each of these lifecycle hooks provides a way to tap into the component’s lifecycle at specific moments in time and perform custom logic or operations as needed. The lifecycle hooks can be implemented as methods within the component class, and Angular will automatically call these methods at the appropriate time.

--

--

Priti Jha
Priti Jha

Written by Priti Jha

Senior front-end developer writing about Angular 8 , Ionic Framework ,Javascript, HTML, Css and all front end technology.

No responses yet