← Back to Angular, page 1

angular-0015 · Easy fresher

What are the main Angular lifecycle hooks, in what order do they run, and what is the difference between the constructor and ngOnInit?

Hooks are methods Angular calls at set moments in a component's life. The main order is constructor, ngOnChanges, ngOnInit, ngDoCheck, ngAfterContentInit, ngAfterViewInit, then ngOnDestroy on removal. The constructor only builds the object and injects dependencies, while ngOnInit runs once after the first inputs are set.

Full answer

Angular creates a component, updates it as data changes, and later destroys it. Hooks let your code run at those moments. Implement the matching interface (OnInit, OnDestroy) and add the method.

Order for one component:

  1. constructor: the class is created. Use it for dependency injection and simple field setup only.
  2. ngOnChanges: runs when a decorator-based @Input changes, before ngOnInit and on each later change. It receives a SimpleChanges object.
  3. ngOnInit: runs once, after the first ngOnChanges. The inputs are ready here. A good place to start loading data.
  4. ngDoCheck: runs on every change detection pass. Use it rarely.
  5. ngAfterContentInit and ngAfterContentChecked: after projected content (<ng-content>) is ready.
  6. ngAfterViewInit and ngAfterViewChecked: after the component's own view and children are ready. View queries are available here.
  7. ngOnDestroy: just before removal. Clean up timers, subscriptions, and listeners.

The constructor versus ngOnInit question: at constructor time, the inputs have not been set. Keep the constructor free of work so tests can build the class easily.

Modern additions. With signal inputs, you do not need ngOnChanges: use computed or effect. For code that must touch the DOM after rendering, use afterNextRender and afterEveryRender (stable from Angular 20) instead of ngAfterViewInit. They do not run on the server, so they are SSR-safe. For cleanup, inject(DestroyRef).onDestroy(...) can replace ngOnDestroy.

typescript
import {
  AfterViewInit,
  Component,
  DestroyRef,
  Input,
  OnChanges,
  OnDestroy,
  OnInit,
  SimpleChanges,
  afterNextRender,
  inject,
} from '@angular/core';

@Component({
  selector: 'app-clock',
  template: `<p>{{ label }}: {{ seconds }}s</p>`,
})
export class ClockComponent implements OnChanges, OnInit, AfterViewInit, OnDestroy {
  @Input() label = 'Timer';
  seconds = 0;
  private timer?: ReturnType<typeof setInterval>;
  private readonly destroyRef = inject(DestroyRef);

  constructor() {
    console.log('1 constructor, label is', this.label);
    afterNextRender(() => console.log('afterNextRender: safe place for DOM work'));
    this.destroyRef.onDestroy(() => console.log('DestroyRef callback'));
  }

  ngOnChanges(changes: SimpleChanges): void {
    console.log('2 ngOnChanges', Object.keys(changes));
  }

  ngOnInit(): void {
    console.log('3 ngOnInit, label is', this.label);
    this.timer = setInterval(() => this.seconds++, 1000);
  }

  ngAfterViewInit(): void {
    console.log('4 ngAfterViewInit');
  }

  ngOnDestroy(): void {
    console.log('5 ngOnDestroy, clearing timer');
    clearInterval(this.timer);
  }
}

Angular 20.3. `OnChanges` is shown with a decorator input on purpose, because that is the only input style that triggers it. Open the console to see the order of log lines.

Key points

  • Order: constructor, ngOnChanges, ngOnInit, ngDoCheck, content hooks, view hooks, ngOnDestroy.
  • Inputs are available in `ngOnInit`, not in the constructor.
  • Clean up in `ngOnDestroy` or with `DestroyRef`.
  • `afterNextRender` is the modern, SSR-safe hook for DOM work.

Common mistakes

  • Reading `@Input` values in the constructor and getting `undefined`.
  • Starting a `setInterval` and never clearing it in `ngOnDestroy`.
  • Doing heavy work in `ngDoCheck`, which runs on every pass.

Follow-ups

  • Why does `ngOnChanges` not run for signal inputs?
  • Where would you access a `viewChild` safely?
  • What is the difference between `ngAfterContentInit` and `ngAfterViewInit`?

Tips

  • Do not recite all eight hooks with no use. Name three or four and say what each is for.
  • Mention `afterNextRender` and `DestroyRef`. This shows modern knowledge.
  • Always mention cleanup. It is what interviewers actually care about.

Evaluation rubric

  • Gives the correct order of main hooks.
  • Explains constructor versus ngOnInit.
  • Mentions cleanup and modern alternatives.