Sub topic 15
Diagnosing views that do not update
This chapter assumes you can build standalone components, use signal and computed, and have read the earlier chapters on enabling zoneless mode, the notifications that schedule change detection, and markForCheck. Here you learn a repeatable method for finding why a view stays stale in a zoneless app, and how to fix each cause.
What a missed update looks like
In a zoneless app, Angular runs change detection only when something tells it to. If your code changes state and nothing sends that notification, the data is correct in memory but the DOM shows the old value. There is usually no error. The symptoms are typically these:
- A value changes in the console or debugger but the page shows the old value.
- The page updates later, when an unrelated event such as a click occurs.
- A test passes only after you add extra waiting or call
detectChanges()manually.
Often the app worked under Zone.js, because Zone.js ran change detection after almost every async task. Zoneless mode removes that safety net, so these bugs show up during migration.
The mental model: who notified Angular?
A zoneless app schedules change detection only when one of these happens:
- A signal read in a template changes.
- A template or host listener handles an event.
markForCheck()is called, which includes theasyncpipe doing so internally.- A component is created or destroyed, or a view is attached or removed.
ComponentRef.setInput()is used.
Anything else is silent. Diagnosis comes down to one question: after the state changed, which of these notifications fired for the component that shows it?
placeholderThat block is not valid, so ignore it. The working method follows.
A diagnostic checklist
Work through these steps in order. Stop at the first one that explains the problem.
- Confirm the state really changed. Log it where it is assigned. If the value never changes, the bug is in your logic and not in change detection.
- Check where the template reads it. A plain field read in a template gives Angular no way to know about changes.
- Check whether the change happened inside an event handler of that template. If so, a notification already fired. If not, continue.
- Check whether the change happened in a callback:
setTimeout,setInterval, a Promise, a WebSocket handler, or a third-party library callback. These do not notify Angular by themselves. - Check whether the component is
OnPush. In zoneless mode, useOnPushanyway. A component that is notOnPushcan still be refreshed incidentally when its parent is checked, which hides bugs. - Check whether the value is mutated in place, such as
items.push(x)on an array held in a signal.
Cause 1: plain fields changed in callbacks
This is the most common cause. The field changes, but no notification is sent.
import { ChangeDetectionStrategy, Component } from '@angular/core';
@Component({
selector: 'app-broken-clock',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Seconds: {{ seconds }}</p>`,
})
export class BrokenClock {
protected seconds = 0;
constructor() {
setInterval(() => {
this.seconds++; // changes, but nothing tells Angular
}, 1000);
}
}The view shows Seconds: 0 forever. The preferred fix is to hold the state in a signal:
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
@Component({
selector: 'app-clock',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Seconds: {{ seconds() }}</p>`,
})
export class Clock {
protected readonly seconds = signal(0);
constructor() {
setInterval(() => this.seconds.update((s) => s + 1), 1000);
}
}If you cannot change the field to a signal, inject ChangeDetectorRef and call markForCheck() after each change, as covered earlier.
Cause 2: mutating a signal's value in place
A signal notifies its readers only when its new value is different from the old one. By default, "different" means a different reference (Object.is). Mutating an object or array inside a signal leaves the reference unchanged.
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
@Component({
selector: 'app-todo-list',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<button (click)="add()">Add</button>
<ul>
@for (item of items(); track item) {
<li>{{ item }}</li>
}
</ul>
`,
})
export class TodoList {
protected readonly items = signal<string[]>([]);
add() {
// Wrong: this.items().push('task'); (same array reference, no update)
this.items.update((list) => [...list, 'task']);
}
}Always produce a new array or object with update() or set(). The same rule applies to computed: if it returns the same reference, dependents are not notified.
Cause 3: reading state outside a reactive context
A signal read inside a template or computed is tracked. A signal read once in a constructor or a plain method and copied into a field is not.
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { UserStore } from './user-store';
@Component({
selector: 'app-greeting',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Hello, {{ name }}</p>`,
})
export class Greeting {
private readonly store = inject(UserStore);
// Copies the value once. Later changes to store.name() are not seen.
protected name = this.store.name();
}Fix it by keeping the signal and calling it in the template ({{ store.name() }}), or by using computed(() => this.store.name()).
Cause 4: callbacks from libraries and the platform
Some code runs outside your control: a charting library, a map widget, a ResizeObserver, an IntersectionObserver, a WebSocket library. Under Zone.js, Angular patched many of these APIs. Now their callbacks need explicit handling. Write the result into a signal inside the callback:
import { ChangeDetectionStrategy, Component, ElementRef, afterNextRender, inject, signal } from '@angular/core';
@Component({
selector: 'app-width-reader',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Width: {{ width() }}px</p>`,
})
export class WidthReader {
private readonly host = inject<ElementRef<HTMLElement>>(ElementRef);
protected readonly width = signal(0);
constructor() {
afterNextRender(() => {
const observer = new ResizeObserver((entries) => {
this.width.set(Math.round(entries[0].contentRect.width));
});
observer.observe(this.host.nativeElement);
});
}
}Writing to the signal is the notification. No NgZone.run() is needed, and in zoneless mode it would not help.
Cause 5: change detection runs but the wrong view is checked
Sometimes a notification fires, yet the visible part of the page is a different component that did not get marked. For example, a service updates a plain object and a child component displays it. Marking the parent does not guarantee the child, an OnPush component, is refreshed unless its own inputs changed or it was marked. Pass data down through signal inputs, or read a shared signal inside the child's own template.
Cause 6: tests that do not wait
A test may fail even though the app works. The test reads the DOM before the scheduled change detection has run. Await stability:
import { TestBed } from '@angular/core/testing';
import { provideZonelessChangeDetection } from '@angular/core';
import { Clock } from './clock';
describe('Clock', () => {
it('renders the initial value', async () => {
TestBed.configureTestingModule({
providers: [provideZonelessChangeDetection()],
});
const fixture = TestBed.createComponent(Clock);
await fixture.whenStable();
expect(fixture.nativeElement.textContent).toContain('Seconds: 0');
});
});Do not paper over missing notifications with fixture.detectChanges(). In zoneless tests, calling it can force a check that the real app would never perform, so the test passes while the app is broken. Using await fixture.whenStable() exercises the real scheduler, so a missed notification fails the test as it would in the browser.
Tools for finding the cause
| Technique | What it tells you |
|---|---|
| `console.log` at the assignment | Whether the state changed at all |
| `ng.getComponent(el)` in the browser console (development mode) | The component instance, so you can compare its fields to the DOM |
| `ng.applyChanges(component)` in the console | Whether forcing a refresh fixes the display, which proves a missing notification |
| An `effect()` that logs a signal | Whether the signal notifies when you expect |
| Angular DevTools, Profiler tab | Which components were checked and when |
| `provideCheckNoChangesConfig({ exhaustive: true, interval: 1000 })` | Development-mode errors for state changed without notification (Angular 20.x and later) |
The ng.applyChanges test is the quickest. If the view is stale, call it. If the view then updates, the state was correct and the only problem was a missing notification.
import { bootstrapApplication } from '@angular/platform-browser';
import { provideZonelessChangeDetection } from '@angular/core';
import { App } from './app/app';
bootstrapApplication(App, {
providers: [provideZonelessChangeDetection()],
});Check this file first when nothing updates anywhere. A page-wide problem often means the provider is missing and zone.js was removed from the build, or the reverse.
A decision flow
Common errors in diagnosis
- Adding
ChangeDetectionStrategy.Defaultback or callingdetectChanges()in many places. This hides the missing notification and slows the app. - Importing
NgZoneand callingrun()in a zoneless app. It no longer schedules change detection. - Assuming that a Promise resolution updates the view. It does so only if the code after it writes a signal or marks the view.
- Forgetting that
markForCheck()marks the view dirty and schedules a check, but does not change the data.
Exercises
Exercise
A component keeps results: string[] = [] as a plain field and fills it with this.http.get(...).subscribe(r => this.results = r). It is OnPush in a zoneless app and the list stays empty. Give two fixes.
Show answer
Store the results in a signal (results = signal<string[]>([]) and call this.results.set(r) in the subscription, reading results() in the template), or call markForCheck() after assigning the field. The async pipe on the observable is a third valid option.
Exercise
A signal holds an array and a button handler runs this.items().push('x'). The list does not update. Why, and how do you fix it?
Show answer
The array reference did not change, so the signal does not notify its readers. Replace the array with a copy: this.items.update(list => [...list, 'x']).
Exercise
A zoneless test passes only when you call fixture.detectChanges() after the action, and fails with await fixture.whenStable(). What does that indicate?
Show answer
The component is missing a change detection notification. detectChanges() forces a check that the real app would not run, while whenStable() relies on the real scheduler. Fix the component so the state change goes through a signal, markForCheck(), or the async pipe.