Component Lifecycle in Angular

  The component lifecycle in Angular defines the different stages a component goes through from its creation to its destruction.


Synopsis:

  In this topic, you will learn about the lifecycle hooksngOnInit and ngOnDestroy.

  • 1. ngOnInit Hook:

      Called after Angular initializes the component's data-bound properties.


  • 2. ngOnDestroy Hook:

      Called just before Angular destroys the component.


  • 3. Constructor:

    Executed every time a component instance is created.


  • 4. ngOnChanges:

    Executed every time an input control's value changes.


  • 5. ngAfterViewInit:

    Executed when the component's view has been fully initialized.


Purpose:


  • Initialize components.
  • Release resources before component destruction.
  • Manage component state.

Exercises


The rest of the content is only available to registered and premium users!



When is the ngOnInit hook called in a component's lifecycle?