Skip to main content

What is the component lifecycle in Angular?

In Angular, the component lifecycle is the sequence of stages from component creation to its destruction, during which Angular calls special lifecycle hook methods to manage state and resources.

Key points

  1. Component creation
  • Angular creates an instance of the component class and injects dependencies through the constructor (constructor).
  • At this stage there is still no access to the DOM elements of the template.
  1. Initialization (OnInit)
  • The ngOnInit() method is called after the input data (@Input) is bound and the template is prepared.
  • Used to initialize logic, load data, and set up the initial state.
  1. Update (OnChanges, DoCheck, AfterViewInit)
  • ngOnChanges(changes) - called when the input data (@Input) changes.
  • ngDoCheck() - a custom change check, used when a more precise reaction to changes is needed.
  • ngAfterViewInit() - called after the child components and DOM elements are initialized.
  1. View update
  • ngAfterViewChecked() - called after each change check in the component and its child components.
  1. Destruction (OnDestroy)
  • The ngOnDestroy() method is called before the component is removed from the DOM.
  • Used to clean up resources, unsubscribe from subscriptions, timers, and events.

In other words, the component lifecycle is a controlled Angular process of creating, updating, and destroying a component, which allows managing logic and resources at every stage.

Short Answer

Interview ready
Premium

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