Sub topic 6
Custom change checks with ngDoCheck
This chapter assumes you know how @Input() properties work, what ngOnChanges does, and that Angular runs change detection to update templates. It also assumes basic TypeScript and RxJS knowledge.
Why ngDoCheck exists
ngOnChanges only fires when Angular sees a new reference in an input. If a parent passes the same array or object and then mutates it, for example with push, the reference stays the same. ngOnChanges does not run, and the child never learns that anything changed.
ngDoCheck fills that gap. Angular calls it on every change detection pass, right after ngOnChanges and ngOnInit on the first pass, and after ngOnChanges on later passes. It receives no arguments, so you compare current state against a copy you saved earlier.
A worked example
The component below receives a list of names. It keeps the last known length and reports when the list grows or shrinks, even though the array reference never changes.
import { Component, DoCheck, Input } from '@angular/core';
@Component({
selector: 'app-name-list',
standalone: true,
template: `
<ul>
@for (name of names; track name) {
<li>{{ name }}</li>
}
</ul>
<p>{{ message }}</p>
`,
})
export class NameListComponent implements DoCheck {
@Input() names: string[] = [];
message = '';
private previousLength = 0;
ngDoCheck(): void {
if (this.names.length !== this.previousLength) {
this.message = `Length changed from ${this.previousLength} to ${this.names.length}`;
this.previousLength = this.names.length;
}
}
}If a parent calls names.push('Ada') on the same array, ngOnChanges stays silent, but ngDoCheck sees the new length and updates message.
Where it sits in the cycle
Keep it cheap
Angular calls ngDoCheck on every pass for every instance of the component. In an app with many components, that means very frequent calls. Follow these rules:
- Compare only a few cheap values, such as a length or a version number.
- Do not make HTTP calls, sort large arrays, or deep-clone objects inside the hook.
- Do not write logs or heavy computations that run on every pass.
- Prefer immutable updates or signals when you control the data. Then
ngOnChangesor signal tracking is enough, and you do not needngDoCheck.
Common mistakes
- Using
ngDoCheckandngOnChangesto do the same job. UsengOnChangesfor reference changes andngDoCheckonly for in-place mutation. - Forgetting to update the saved copy. If
previousLengthis never updated, the branch runs on every pass. - Expecting
ngDoCheckto run only when something changed. It runs whenever the parent is checked, even if nothing changed.
Exercise
A parent mutates an array passed to a child with items.push(x). Why does the child's ngOnChanges not run, and which hook can detect the change?
Show answer
ngOnChanges runs only when the input's reference changes, and push keeps the same array reference. ngDoCheck runs on every change detection pass, so the child can compare the array's current length to a stored previous length and react.
Exercise
Why should ngDoCheck avoid sorting a large array each time it runs?
Show answer
Angular calls ngDoCheck on every change detection pass for each instance of the component, so expensive work repeats very often and slows the whole application.