Sub topic 12
How hooks interleave between parent and child components
This chapter assumes you know each hook on its own (ngOnChanges, ngOnInit, ngDoCheck, the content and view hooks, ngOnDestroy) and the single-component order from the previous chapter. Here you will see how those sequences combine when one component sits inside another. This is where most "why is my data undefined here?" questions come from.
The rule behind the order
Angular does not run a component's whole lifecycle and then move on to the next component. It walks the tree and refreshes one view at a time. For each view, it runs hooks in these stages:
- The initialization hooks (
ngOnChanges,ngOnInit,ngDoCheck) of the components and directives declared in that view. They run as the template's bindings are updated, in template order. - The content hooks (
ngAfterContentInit,ngAfterContentChecked) of those same directives. - A refresh of each child component's own view. This is a recursive repeat of the same stages.
- The view hooks (
ngAfterViewInit,ngAfterViewChecked) of those directives.
Two consequences follow:
- Top-down on the way in. A parent's
ngOnChanges,ngOnInitandngDoCheckrun before the same hooks of its children. - Bottom-up on the way out. A parent's view hooks run only after every child view inside it has been refreshed. So a child's
ngAfterViewInitruns before its parent's.
Observing the order
This example logs every hook. The shared base class keeps the example short. Angular calls lifecycle hooks that a component inherits from an undecorated base class.
import {
AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit,
Component, DoCheck, Input, OnChanges, OnDestroy, OnInit,
} from '@angular/core';
abstract class TracedHooks
implements OnChanges, OnInit, DoCheck, AfterContentInit,
AfterContentChecked, AfterViewInit, AfterViewChecked, OnDestroy {
protected abstract readonly name: string;
private log(hook: string): void {
console.log(`${this.name}.${hook}`);
}
ngOnChanges(): void { this.log('ngOnChanges'); }
ngOnInit(): void { this.log('ngOnInit'); }
ngDoCheck(): void { this.log('ngDoCheck'); }
ngAfterContentInit(): void { this.log('ngAfterContentInit'); }
ngAfterContentChecked(): void { this.log('ngAfterContentChecked'); }
ngAfterViewInit(): void { this.log('ngAfterViewInit'); }
ngAfterViewChecked(): void { this.log('ngAfterViewChecked'); }
ngOnDestroy(): void { this.log('ngOnDestroy'); }
}
@Component({
selector: 'app-child',
standalone: true,
template: `<p>Child: {{ label }}</p>`,
})
export class ChildComponent extends TracedHooks {
protected readonly name = 'Child';
@Input() label = '';
constructor() {
super();
console.log('Child constructor');
}
}
@Component({
selector: 'app-parent',
standalone: true,
imports: [ChildComponent],
template: `<app-child [label]="title" />`,
})
export class ParentComponent extends TracedHooks {
protected readonly name = 'Parent';
title = 'hello';
constructor() {
super();
console.log('Parent constructor');
}
}If ParentComponent is the bootstrapped root component, the first change detection pass logs the lines below. Parent.ngOnChanges is absent because the root has no bound inputs.
Parent constructor
Child constructor
Parent.ngOnInit
Parent.ngDoCheck
Parent.ngAfterContentInit
Parent.ngAfterContentChecked
Child.ngOnChanges
Child.ngOnInit
Child.ngDoCheck
Child.ngAfterContentInit
Child.ngAfterContentChecked
Child.ngAfterViewInit
Child.ngAfterViewChecked
Parent.ngAfterViewInit
Parent.ngAfterViewCheckedRead the output against the four stages:
- Both constructors run first. Angular creates the whole static template in a creation pass before the first update pass. This is why constructors can never see input values.
- The parent's
ngAfterContentInitruns before the child'sngOnInit. The parent's content hooks belong to the view that contains the parent. They run before Angular refreshes the parent's own view, where the child lives. - The child finishes completely, including its view hooks, before the parent's view hooks run.
Later passes
After the first pass, the Init hooks are gone and ngOnChanges runs only when a bound input changed.
| Hook | Runs again on later passes? |
|---|---|
| `ngOnChanges` | Only when one of that component's inputs changed |
| `ngOnInit` | Never |
| `ngDoCheck` | Every pass |
| `ngAfterContentInit` | Never |
| `ngAfterContentChecked` | Every pass |
| `ngAfterViewInit` | Never |
| `ngAfterViewChecked` | Every pass |
The relative order stays the same. A plain pass with no input changes logs Parent.ngDoCheck, Parent.ngAfterContentChecked, Child.ngDoCheck, Child.ngAfterContentChecked, Child.ngAfterViewChecked, Parent.ngAfterViewChecked.
With OnPush, a child whose inputs did not change and which is not marked dirty skips its view refresh. Its ngDoCheck still runs, because Angular calls it as part of the parent's refresh. Its checked hooks do not run when its view is skipped.
Siblings and projected content
Siblings run their hooks in stages, not one after another. With <app-a /> followed by <app-b /> in a parent's template, you get:
A.ngOnChanges,A.ngOnInit,A.ngDoCheck, then the same three for B.A.ngAfterContentInit,A.ngAfterContentChecked, then the same two for B.- The views of A and B are refreshed, including any grandchildren.
A.ngAfterViewInit,A.ngAfterViewChecked, then the same two for B.
So B's ngOnInit runs before A's ngAfterViewInit. Do not assume a sibling "finished" before the next one starts.
Projected content follows the same rule, applied to where each component is declared. Take <app-card><app-item /></app-card> written in ParentComponent's template. Both Card and Item are declared in the parent's view. Their ngOnInit hooks therefore run during the same stage, in template order. Then Card.ngAfterContentInit runs, and Angular guarantees that the projected Item has been initialized by then. That is the reason the AfterContent hooks exist.
When a child appears inside @if or @for, its constructor does not run in the creation pass. Angular creates it when the block's embedded view is created during the update pass, so its constructor runs after the parent's own ngOnInit and ngDoCheck. Its hooks then follow the same stages.
Practical consequences
- Parent data in the child. A parent can prepare values in
ngOnInit, and the child sees them as inputs in its ownngOnChangesandngOnInit. The parent's hooks run first. - Child data in the parent. A parent cannot read a
@ViewChildcomponent's state from its ownngOnInit, because the child has not initialized. UsengAfterViewInit, which runs after the child's view hooks. - Writing upward. A child that changes a parent property from its
ngOnInitorngAfterViewInitcan triggerExpressionChangedAfterItHasBeenChecked. The parent's bindings earlier in its template, or the whole parent view, may already have been checked in this pass. Prefer anoutput, a shared signal or service, or scheduling the change for after the pass. - Destruction. When a parent is removed, Angular destroys its view tree. In current versions, a child's
ngOnDestroyruns before its parent's.
Exercise
A parent template contains <app-a /> and then <app-b />. Both components log from ngOnInit and ngAfterViewInit. In what order do the four lines appear on the first pass, and why can B's ngOnInit not rely on A's view being ready?
Show answer
The order is A.ngOnInit, B.ngOnInit, A.ngAfterViewInit, B.ngAfterViewInit. Angular runs the initialization hooks of every directive in the parent's view while it updates the template, in template order. Only afterwards does it refresh the child views and run the view hooks. When B's ngOnInit runs, A's view has not been created or checked yet, so B cannot rely on A's view being ready.
Exercise
A child component calls this.parent.count++ in its ngAfterViewInit, and the parent template shows {{ count }}. Why can this throw ExpressionChangedAfterItHasBeenChecked, and what is a safer design?
Show answer
The child's ngAfterViewInit runs after the parent's template bindings, including {{ count }}, were evaluated in the current pass. In development mode Angular runs a second check, sees a different value, and throws. A safer design is for the child to emit an output (or update a shared signal) so the parent changes its own state in a normal change detection cycle.