Sitemap

Member-only story

Lifecycle Hooks in Angular

2 min readDec 11, 2024

--

Angular’s lifecycle hooks are methods in Angular components and directives that allow you to tap into key moments in the lifecycle of a component or directive. Below is a description of the main lifecycle hooks with their execution order and a graph to illustrate their flow:

Key Lifecycle Hooks in Angular

  1. ngOnChanges:
  • Triggered when the input properties of a component or directive are changed.
  • Receives a SimpleChanges object containing the previous and current values of the bound inputs.

2. ngOnInit:

  • Called once, after the first ngOnChanges.
  • Used for initialization logic that requires input properties to be set.

3. ngDoCheck:

  • Called during every change detection run, after ngOnChanges and ngOnInit.
  • Used to detect changes that Angular does not.

4. ngAfterContentInit:

Called once after the content (e.g., <ng-content>) is projected into the component.

  • Used for logic that depends on projected content.

5. ngAfterContentChecked:

Called after every check of the projected content.

  • Used for logic that depends on content changes.

6. ngAfterViewInit:

--

--

Chandan Kumar
Chandan Kumar

Written by Chandan Kumar

Full Stack Developer (MEAN & .NET) | Angular, Node.js, C#, SQL Server | 4.5+ Years of Experience in Web Dev https://www.linkedin.com/in/developerchandan