Skip to main content

What lifecycle hooks exist in Angular?

In Angular, there are several lifecycle hooks, which let you react to a component's creation, update, and destruction:

Lifecycle hooks

  1. ngOnChanges(changes: SimpleChanges)
  • Called when the input data (@Input) changes.
  • Receives a changes object with the previous and current values.
  1. ngOnInit()
  • Called once after the input properties are first initialized.
  • Used for initial component setup and loading data.
  1. ngDoCheck()
  • Called on every Angular change detection check.
  • Allows implementing custom change-tracking logic.
  1. ngAfterContentInit()
  • Called after the projection of external content (<ng-content>).
  • Suitable for working with content inserted from the parent component.
  1. ngAfterContentChecked()
  • Called after every check of the projected content.
  1. ngAfterViewInit()
  • Called after the component's view and its child components are initialized.
  • It is safe to work with DOM elements through ViewChild.
  1. ngAfterViewChecked()
  • Called after every check of the component's view and child components.
  1. ngOnDestroy()
  • Called before the component is destroyed.
  • Used to clean up resources: unsubscribing from subscriptions, timers, events.

These hooks let you precisely control the component's behavior at every stage of its life and manage resources and data.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.