Sub topic 9
Stale views: common mistakes and how to debug them
This chapter assumes you know how an OnPush component decides to refresh. That means input reference changes, events in its own template, the async pipe, markForCheck, and signals read in the template. Here we look at what goes wrong when one of those is missing, and how to find the cause.
The symptom
A stale view looks like this. The data in your class is correct, and a console.log shows the new value. The screen still shows the old one, and it may refresh later after an unrelated click. That delay is the clue. Something eventually marked the component dirty, but your change did not.
Mistake 1: mutating an input
This is the most common cause. The parent changes an array or object in place, so the reference passed to the child stays the same.
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
@Component({
selector: 'app-todo-list',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<ul>
@for (todo of todos; track todo) {
<li>{{ todo }}</li>
}
</ul>
`,
})
export class TodoListComponent {
@Input({ required: true }) todos: string[] = [];
}
@Component({
selector: 'app-todo-page',
imports: [TodoListComponent],
template: `
<app-todo-list [todos]="todos" />
<button (click)="addBroken()">Add (broken)</button>
<button (click)="addFixed()">Add (fixed)</button>
`,
})
export class TodoPageComponent {
todos: string[] = ['Write tests'];
addBroken() {
// Same array reference: the OnPush child is not checked.
this.todos.push('Ship');
}
addFixed() {
// New array: the input reference changes, so the child refreshes.
this.todos = [...this.todos, 'Ship'];
}
}The click happens in the parent's template, so the parent is checked. The child is a separate OnPush component. Angular compares todos with ===, finds the same array, and skips the child.
Mistake 2: async updates that nobody announces
A subscription callback, setTimeout, or a promise can change a plain field. Zone.js still runs a change detection pass afterwards. But the OnPush component is not marked dirty, so that pass skips it.
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
@Component({
selector: 'app-status',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Status: {{ status() }}</p>`,
})
export class StatusComponent {
status = signal('loading');
constructor() {
setTimeout(() => this.status.set('ready'), 2000);
}
}With a plain status = 'loading' field, the timeout would update the field and leave the screen unchanged. Making it a signal fixes this, because the template read tells Angular when to refresh. Other fixes are the async pipe or markForCheck(), as covered in earlier chapters.
Mistake 3: other causes
- Event in the wrong place. A click handler in a parent's template does not mark an OnPush child dirty. Only events in the child's own template (or its outputs) do.
- Forgotten
detach(). A detachedChangeDetectorRefis skipped until you callreattach()or rundetectChanges()yourself. - Non-reactive state in a service. A component reads
service.countdirectly in its template, and the service changes it from a place the component never hears about. Expose a signal or an Observable instead. - Mutating inside a nested object. Copying the top-level object but reusing a nested array only fixes part of the problem, because children of the child may still hold the old reference.
Debugging checklist
Work through it in this order:
- Switch to Default temporarily. Comment out
changeDetection: ChangeDetectionStrategy.OnPush. If the view now updates, change detection scheduling is the cause. If not, the bug is in your data. - Check the reference. In the parent, log the value before and after the change. If it is the same object, you have a mutation.
- Inspect in the browser. In a development build, select the element in DevTools and run
ng.getComponent($0)in the console to read the live component state. Compare it with the screen. Runningng.applyChanges(ng.getComponent($0))forces a refresh. If the screen then corrects itself, you have confirmed a missed notification. - Use Angular DevTools. The profiler records which components were checked during each pass, so you can see that your component was skipped.
Prevent it
Treat inputs as read-only and replace them with [...items], { ...obj }, map, or filter. Keep state that the template reads in signals or Observables used with the async pipe. Use markForCheck() only for the rare callback that cannot be expressed either way.
Exercise
A parent component calls this.filters.active = true on an object it passes to an OnPush child as [filters]="filters". The child never updates, but a click on a button inside the child makes it show the right value. Why, and what is the fix?
Show answer
The mutation keeps the same object reference, so the input check sees no change and skips the child. The click inside the child marks it dirty, so it is then checked and shows the already-changed value. Fix it by replacing the object in the parent: this.filters = { ...this.filters, active: true }.