Skip to main content

Overview

Lifecycle hooks provide visibility into key moments in a component or directive’s lifecycle, from creation to destruction. Angular calls these hook methods in a specific sequence.

Lifecycle Sequence

  1. Constructor - Class instantiation (not a hook)
  2. OnChanges - Input property changes
  3. OnInit - Initialization after first change detection
  4. DoCheck - Custom change detection
  5. AfterContentInit - Content projection initialized (once)
  6. AfterContentChecked - Content projection checked
  7. AfterViewInit - View initialized (once)
  8. AfterViewChecked - View checked
  9. OnDestroy - Cleanup before destruction

OnChanges

Interface

Description

Called when any data-bound input property changes. Receives a SimpleChanges object containing current and previous values.

Parameters

SimpleChanges
Object containing changed properties with their current and previous values.

Example

user-profile.component.ts
ngOnChanges is called before ngOnInit and whenever input properties change. It’s not called for internal state changes.

OnInit

Interface

Description

Called once after the first ngOnChanges. Used for component initialization logic.

Example

data-list.component.ts
Use ngOnInit for initialization logic that requires input properties or depends on services. Keep constructors lightweight.

DoCheck

Interface

Description

Called during every change detection cycle. Use for custom change detection logic.

Example

custom-check.component.ts
ngDoCheck is called very frequently. Keep its logic lightweight to avoid performance issues.

AfterContentInit

Interface

Description

Called once after Angular projects external content into the component’s view (content children).

Example

tab-container.component.ts

AfterContentChecked

Interface

Description

Called after Angular checks the content projected into the component.

Example

content-wrapper.component.ts

AfterViewInit

Interface

Description

Called once after Angular initializes the component’s views and child views.

Example

chart.component.ts
View children are guaranteed to be initialized in ngAfterViewInit. Don’t try to access them in ngOnInit.

AfterViewChecked

Interface

Description

Called after Angular checks the component’s views and child views.

Example

scroll-monitor.component.ts
Be cautious with ngAfterViewChecked - it’s called very frequently. Avoid expensive operations or state changes that trigger additional change detection.

OnDestroy

Interface

Description

Called just before Angular destroys the component or directive. Used for cleanup logic.

Example

timer.component.ts
Always clean up in ngOnDestroy:
  • Unsubscribe from Observables
  • Clear intervals and timeouts
  • Detach event listeners
  • Cancel pending HTTP requests

Complete Lifecycle Example

full-lifecycle.component.ts

Hook Call Frequency

Best Practices

  • Use ngOnInit for most initialization logic
  • Keep ngDoCheck, ngAfterContentChecked, and ngAfterViewChecked lightweight
  • Always implement ngOnDestroy for components with subscriptions
  • Use ngAfterViewInit to access child components and DOM elements
  • Prefer ngOnChanges over getters/setters for input change detection
  • Don’t modify component state in ngAfterViewChecked (causes ExpressionChangedAfterItHasBeenCheckedError)
  • Avoid heavy computations in frequently called hooks
  • Don’t forget to unsubscribe from Observables in ngOnDestroy
  • Be careful with async operations in lifecycle hooks

Signal-Based Alternative

With Angular signals, many lifecycle hooks can be replaced:
signal-component.ts

See Also

Lifecycle Guide

Component lifecycle guide

Change Detection

How change detection works

Queries

Query child components

Signals

Modern reactive patterns