Sub topic 7
What schedules change detection now
This chapter assumes you know standalone components, the default zone-based change detection model, OnPush, and basic signal and computed use. It also assumes zoneless mode is already enabled with provideZonelessChangeDetection(), which the previous chapter covered. The question here is what makes Angular run change detection once Zone.js is gone.
The scheduler replaces the zone
In a zone-based app, Zone.js patches browser APIs. After any patched task finishes (a click, a timer, a resolved promise), NgZone emits onMicrotaskEmpty, and Angular runs a full change detection pass from the root.
A zoneless app has no such signal. Angular instead has an internal change detection scheduler. Parts of the framework tell the scheduler "something changed", and the scheduler runs ApplicationRef.tick() at a convenient moment. Code that Angular does not know about, such as a plain setTimeout callback, notifies nobody, and the scheduler stays idle.
Two properties of the scheduler matter in practice:
- Coalescing. Several notifications close together produce one pass. The scheduler waits for whichever comes first, a
setTimeoutor arequestAnimationFrame, and then runs once. - Dirty-based checking. A notification marks views dirty, the same way
markForCheck()does underOnPush. The pass then refreshes only dirty views and their ancestors, not the whole tree.
The notification sources
Angular schedules change detection in these cases:
| Source | What happens |
|---|---|
| A signal read in a template changes | The reactive consumer for that view is marked dirty and the scheduler is notified. |
| `ChangeDetectorRef.markForCheck()` | The view and its ancestors are marked dirty and a pass is scheduled. |
| The `async` pipe receives a new value | The pipe calls `markForCheck()` internally. |
| `ComponentRef.setInput()` | The input is set and the view is marked dirty. |
| A template or host listener runs | Angular wraps the handler and marks the view dirty afterwards. |
| A view is attached or removed | Creating a component through `ViewContainerRef`, or attaching a view that was marked dirty, schedules a pass. |
Everything else is invisible to Angular: a changed plain field, a mutated object, or a value assigned in a callback Angular did not register.
Signals
Signals are the primary source. When a template reads count(), Angular registers that view as a reactive consumer of the signal. When the signal's value changes, the consumer is marked dirty and the scheduler is notified. This needs no markForCheck() call and no zone.
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
@Component({
selector: 'app-ticker',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Ticks: {{ ticks() }}</p>`,
})
export class Ticker {
protected readonly ticks = signal(0);
constructor() {
setInterval(() => this.ticks.update((n) => n + 1), 1000);
}
}The timer is not patched by Zone.js. The view still updates every second, because the signal write is the notification. The timer only decides when the write happens.
Two details are worth knowing:
- Writing a signal to the value it already holds does nothing. Signals compare with
Object.isby default, soitems.set(sameArray)after mutating the array in place produces no update. Useupdatewith a new array, orsetwith a copy. - A
computedfeeds the view only if the template reads it. A signal that no template reads does not schedule anything, which is correct, since nothing visible depends on it.
Template and host listeners
Event bindings such as (click)="save()" are registered by Angular, so Angular knows when they run. After the handler returns, Angular marks the component's view dirty and notifies the scheduler. This works even if the handler only changes a plain field.
import { ChangeDetectionStrategy, Component } from '@angular/core';
@Component({
selector: 'app-toggle',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<button (click)="flip()">{{ label }}</button>`,
})
export class Toggle {
protected label = 'Off';
flip() {
this.label = this.label === 'Off' ? 'On' : 'Off';
}
}This component works without signals. The click notification covers the synchronous change to label.
The coverage ends when the work leaves the handler. If flip() assigned label inside a setTimeout callback, the click notification would already have been consumed by the time the field changed. The view would show the old value until something else triggered a pass.
Listeners attached by hand, with element.addEventListener or fromEvent, are not template listeners. Angular does not wrap them, so they notify nothing.
The async pipe and markForCheck
The async pipe subscribes to an observable or promise. When it emits, the pipe stores the value and calls markForCheck(). That is why code using async usually keeps working after the move to zoneless mode.
For state you cannot move into a signal, call markForCheck() yourself, as shown in the LegacyTimer example from the spine. The next chapter covers it in detail. Here, remember only that it belongs in the same category as signals and the async pipe: an explicit statement to Angular that this view needs checking.
For observables you subscribe to manually, toSignal() is usually cleaner than markForCheck(), because it turns emissions into signal writes:
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { interval } from 'rxjs';
@Component({
selector: 'app-seconds',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Seconds: {{ seconds() }}</p>`,
})
export class Seconds {
protected readonly seconds = toSignal(interval(1000), { initialValue: 0 });
}setInput and dynamic components
When a parent template binds [title]="name()", Angular updates the child input during the parent's refresh, so no extra step is needed. For components you create in code, assigning ref.instance.title = 'Hi' is a plain field write that nothing observes. Use setInput:
import { Component, ViewContainerRef, inject, input } from '@angular/core';
@Component({
selector: 'app-badge',
template: `<span>{{ title() }}</span>`,
})
export class Badge {
readonly title = input.required<string>();
}
@Component({
selector: 'app-host',
template: ``,
})
export class Host {
private readonly container = inject(ViewContainerRef);
constructor() {
const ref = this.container.createComponent(Badge);
ref.setInput('title', 'Hello');
}
}setInput writes the input and marks the view dirty. It also works for @Input() properties, and it respects OnPush. Creating the component also attaches a view, which is itself a notification source.
What does not notify
These common patterns fail silently in a zoneless app:
- Assigning a plain field in
setTimeout,setInterval, or aPromise.thencallback. - Assigning a plain field in a manual
subscribecallback (for example anHttpClientcall). - Mutating an object or array held in a signal without producing a new reference.
- Updating state from a third-party library callback that Angular did not register.
- Calling
NgZone.run()and expecting a pass. Without Zone.js,runsimply calls the function.
The symptom is always the same: the data is correct, the DOM is stale, and an unrelated click later fixes it. That last observation is a clue. The click is a listener notification, and it refreshes the dirty views along with everything else.
Forcing a pass
ApplicationRef.tick() runs a pass for the whole app and ChangeDetectorRef.detectChanges() runs one for a view and its children. Both work in zoneless mode, but they are escape hatches. Calling them from many places creates ordering problems and repeated work, because the scheduler would already have coalesced those updates. Prefer a signal. Fall back to markForCheck() when you cannot use one.
Exercise
A component has protected message = 'Loading'; and a template {{ message }}. In the constructor, setTimeout(() => { this.message = 'Done'; }, 500); runs. The component uses OnPush in a zoneless app. The view stays on "Loading". Give two fixes and say why each one notifies Angular.
Show answer
Fix 1: change message to signal('Loading'), read message() in the template, and call this.message.set('Done') in the timer. The template read makes the view a consumer of the signal, so the write marks it dirty and notifies the scheduler. Fix 2: inject ChangeDetectorRef and call markForCheck() right after assigning this.message = 'Done'. That marks the view and its ancestors dirty and schedules a pass. The plain assignment alone fails because a timer callback is not a template listener, and nothing else tells Angular.
Exercise
A button has (click)="load()". load() sets this.status = 'Loading' synchronously, then calls fetch(...).then(() => { this.status = 'Ready'; }) with status a plain field. Which assignment updates the view without extra work, and which does not?
Show answer
The synchronous 'Loading' assignment shows up, because the click listener marks the view dirty when the handler returns. The 'Ready' assignment inside then runs later, outside any listener, so no notification occurs and the view stays on "Loading". Making status a signal fixes both.