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:
constructor: the class is created. Use it for dependency injection and simple field setup only.ngOnChanges: runs when a decorator-based@Inputchanges, beforengOnInitand on each later change. It receives aSimpleChangesobject.ngOnInit: runs once, after the firstngOnChanges. The inputs are ready here. A good place to start loading data.ngDoCheck: runs on every change detection pass. Use it rarely.ngAfterContentInitandngAfterContentChecked: after projected content (<ng-content>) is ready.ngAfterViewInitandngAfterViewChecked: after the component's own view and children are ready. View queries are available here.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.
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.