DEV Community

Cover image for Understanding Angular Component Lifecycles
Dipak Ahirav
Dipak Ahirav

Posted on

Understanding Angular Component Lifecycles

Angular is a powerful framework for building dynamic web applications, and understanding its component lifecycle hooks can significantly enhance your ability to create efficient and effective applications. This blog will explore the lifecycle of an Angular component, detailing each stage and providing examples to help you grasp these concepts thoroughly.

Introduction to Component Lifecycle

In Angular, every component goes through a series of lifecycle events that you can hook into to perform various actions at specific times in the life of the component. These lifecycle hooks offer visibility into key moments in the component's lifecycle, such as creation, rendering, data updates, and destruction.

Key Lifecycle Hooks

Here’s an overview of the most commonly used lifecycle hooks in Angular:

1. ngOnInit

  • Purpose: Initialize the component.
  • When it runs: After the first ngOnChanges and the component's data-bound properties have been checked.
  • Use cases: Fetch data required for the component, set up initial values.

2. ngOnChanges

  • Purpose: Respond when Angular sets or resets data-bound input properties.
  • When it runs: Before ngOnInit and whenever one or more data-bound input properties change.
  • Use cases: React to changes in input properties that may affect component state.

3. ngDoCheck

  • Purpose: Detect and act upon changes that Angular can't or won't detect on its own.
  • When it runs: During every change detection run, immediately after ngOnChanges and ngOnInit.
  • Use cases: Implement custom change detection or when your app requires updates during every tick.

4. ngAfterContentInit

  • Purpose: Respond after Angular projects external content into the component’s view.
  • When it runs: After the first ngDoCheck.
  • Use cases: Manipulate or query DOM of the projected content.

5. ngAfterContentChecked

  • Purpose: Respond after the projected content has been checked.
  • When it runs: After ngAfterContentInit and every subsequent ngDoCheck.
  • Use cases: Work with the content checked by Angular.

6. ngAfterViewInit

  • Purpose: Respond after Angular initializes the component's views and child views.
  • When it runs: After the first ngAfterContentChecked.
  • Use cases: Initialize or access view children, query DOM elements.

7. ngAfterViewChecked

  • Purpose: Respond after the component's view, and child views have been checked.
  • When it runs: After the ngAfterViewInit and every subsequent ngAfterContentChecked.
  • Use cases: Actions after the view has been updated by Angular.

8. ngOnDestroy

  • Purpose: Cleanup just before Angular destroys the component.
  • When it runs: Just before Angular destroys the component.
  • Use cases: Unsubscribe from observables, detach event handlers to avoid memory leaks.

Example: Tracking Input Changes

Here’s a simple example demonstrating how to use ngOnChanges to monitor input property changes:

import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';

@Component({
  selector: 'app-sample',
  template: `<p>{{message}}</p>`
})
export class SampleComponent implements OnChanges {
  @Input() message: string;

  ngOnChanges(changes: SimpleChanges) {
    if (changes['message']) {
      console.log('Message changed:', changes['message'].currentValue);
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Understanding Angular's component lifecycle hooks can profoundly impact the performance and behavior of your applications. By leveraging these hooks, developers can create more dynamic, responsive, and efficient web applications. Practice incorporating these hooks into your components to see their benefits in action.

Top comments (0)