Full answer
Order matters in interviews because it explains why some values are not available yet.
For one component the sequence is: constructor, ngOnChanges (only if it has inputs that were set), ngOnInit, ngDoCheck, ngAfterContentInit, ngAfterContentChecked, ngAfterViewInit, ngAfterViewChecked. ngOnChanges and ngOnInit run once per input change and once per creation. ngDoCheck and the two Checked hooks run on every change detection pass. ngOnDestroy runs when the component is removed.
With a parent and a child in the parent's template, the order was captured from a real run (Angular 20, one parent, one child with one input):
- Parent constructor, then child constructor. Angular creates the parent's view first, and that creates the child object, before any
ngOnInitruns. - Parent
ngOnInit, then parentngAfterContentInit(the parent has no projected content, so this comes right away). - Child
ngOnChanges,ngOnInit,ngAfterContentInit. Input values reach the child during the parent's update pass. - Child
ngAfterViewInit. - Parent
ngAfterViewInit.
Do not memorise every step. Remember three rules: constructors run top down and early, ngOnInit runs top down, and the "after view" hooks run bottom up.
So initialisation goes top down, and the "after view" hooks go bottom up. The parent's ngAfterViewInit is the first place where viewChild queries for children are filled and the children are fully rendered.
With content projection (<app-card><app-badge /></app-card>), the projected app-badge is created by the host template's component, and the card's ngAfterContentInit runs after the badge exists. The card's own view hooks run after that.
Practical consequences:
- Do not read inputs in the constructor. They are set before
ngOnInit. - Do not read
viewChildbeforengAfterViewInit(signal queries are available after the first render). - Changing a parent value inside a child's
ngAfterViewInitcan causeExpressionChangedAfterItHasBeenCheckedin development mode.
import { AfterContentInit, AfterViewInit, Component, OnChanges, OnInit, input } from '@angular/core';
export const log: string[] = [];
@Component({ selector: 'app-child', template: `child {{ label() }}` })
export class ChildComponent implements OnChanges, OnInit, AfterContentInit, AfterViewInit {
readonly label = input('');
constructor() { log.push('child constructor'); }
ngOnChanges(): void { log.push('child ngOnChanges'); }
ngOnInit(): void { log.push('child ngOnInit'); }
ngAfterContentInit(): void { log.push('child ngAfterContentInit'); }
ngAfterViewInit(): void { log.push('child ngAfterViewInit'); }
}
@Component({
selector: 'app-parent',
imports: [ChildComponent],
template: `<app-child label="a" />`,
})
export class ParentComponent implements OnInit, AfterContentInit, AfterViewInit {
constructor() { log.push('parent constructor'); }
ngOnInit(): void { log.push('parent ngOnInit'); }
ngAfterContentInit(): void { log.push('parent ngAfterContentInit'); }
ngAfterViewInit(): void { log.push('parent ngAfterViewInit'); }
}
// Captured order for one parent with one child (Karma, Angular 20.3):
// parent constructor, child constructor, parent ngOnInit, parent ngAfterContentInit,
// child ngOnChanges, child ngOnInit, child ngAfterContentInit,
// child ngAfterViewInit, parent ngAfterViewInitAngular 20.3. The components log each hook. The order in the code comment is the one captured from a Karma run in headless Chrome, and an assertion in that test checks it.
Key points
- Per component: constructor, ngOnChanges, ngOnInit, ngDoCheck, content hooks, view hooks.
- Constructors and ngOnInit run parent first. Child "after view" hooks finish before the parent's.
- Projected content is ready before the host's `ngAfterContentInit`.
- Read inputs in `ngOnInit`, and view queries after view init.
Common mistakes
- Reading `@Input` values in the constructor.
- Expecting the parent's `ngAfterViewInit` to run before the child's.
- Changing bound state in an `After...Checked` hook without care.
Follow-ups
- Why does `ngOnChanges` not run for a component with no inputs?
- What causes ExpressionChangedAfterItHasBeenChecked?
- Where would you start a timer and where would you stop it?
Tips
- Draw the two-level sequence in your answer.
- Say which hooks repeat on every pass.
- Link the order to a real bug (reading a query too early).
Evaluation rubric
- Gives the correct per-component sequence.
- Gets parent and child ordering right.
- Explains consequences for inputs and queries.