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
ngOnChanges(changes: SimpleChanges)
- Called when the input data (
@Input) changes. - Receives a
changesobject with the previous and current values.
ngOnInit()
- Called once after the input properties are first initialized.
- Used for initial component setup and loading data.
ngDoCheck()
- Called on every Angular change detection check.
- Allows implementing custom change-tracking logic.
ngAfterContentInit()
- Called after the projection of external content (
<ng-content>). - Suitable for working with content inserted from the parent component.
ngAfterContentChecked()
- Called after every check of the projected content.
ngAfterViewInit()
- Called after the component's view and its child components are initialized.
- It is safe to work with DOM elements through
ViewChild.
ngAfterViewChecked()
- Called after every check of the component's view and child components.
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 readyPremium
A concise answer to help you respond confidently on this topic during an interview.