الانتقال إلى المحتوى
FrontendAngular· 8 دقائق للقراءة

Angular Doesn't Watch Your Variables: How Change Detection Really Works

Angular re-renders in passes, and in Angular 22 only a notification schedules one. A plain assignment isn't one.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • Angular 22
محتويات المقال

A developer moves a "Save draft" button from an older app into a new Angular 22 project. Clicking it shows "Saving..." right away, as before. Then the request finishes and the label stays on "Saving..." for good. A console.log in the promise callback prints Saved, so the field holds the right value. The screen just never hears about it.

The common misconception

The belief: Angular watches your component's fields, so this.status = 'Saved' is all it takes, and the DOM updates the moment the value changes.

For years, that's exactly what it looked like. With zone.js, which captures async operations like setTimeout, network requests and event listeners, Angular ran change detection after each of them, so an assignment in a callback reached the screen almost instantly. Some other frameworks really do track property writes, through proxies or compiled setters, which makes the model feel universal. The half-truth is that the screen did update after async work. A watcher was never the reason, though; zone.js was, and Angular 22 apps don't load it by default.

What actually happens

Change detection is a pass, not a watcher

Angular remembers the last value each template binding rendered. A pass walks the component tree from the top. For each component it checks, it re-evaluates the template's expressions, compares each result with the remembered value, and updates only the DOM nodes whose value changed. Between passes, Angular doesn't look at your fields at all. this.status = 'Saved' is a plain JavaScript property write: no proxy, no setter, nothing that tells Angular it happened.

Something has to schedule the pass

So the question is never "did the value change?" but "did anything tell Angular to check?" With zone.js, the answer was yes after almost every async task. Since Angular 21, new apps are by default, and Angular schedules a pass only when it receives one of these notifications:

  • a bound template or host listener ran, like (click);
  • a signal that a template reads was updated;
  • markForCheck() was called, which the async pipe does for you;
  • ComponentRef.setInput() set an input;
  • a view that one of the above marked dirty was attached.

A promise resolving, a setTimeout firing or a WebSocket message arriving isn't on the list.

OnPush decides which components get checked

A scheduled pass doesn't check everything either. Since Angular 22, components default to OnPush, and a pass checks an OnPush component only when it was marked: a signal its template reads changed, it received a new input value, or an event fired in it or one of its children. Unmarked components are skipped. The old check-on-every-pass behavior still exists as ChangeDetectionStrategy.Eager, the new name for the deprecated Default.

TypeScript save-button.ts
import { Component, inject } from '@angular/core';@Component({  selector: 'app-save-button',  template: `    <button (click)="save()">Save draft</button>    <span>{{ status }}</span>  `,})export class SaveButton {  private readonly drafts = inject(DraftApi);  status = '';  save(): void {    this.status = 'Saving...'; // shows: the click schedules a pass    this.drafts.save().then(() => {      this.status = 'Saved'; // never shows: nothing schedules a pass    });  }}

In the first version, "Saving..." appears because the click listener marks the component and schedules a pass, which runs after the handler returns. When the promise resolves, the assignment changes the field and nothing else happens: no notification, no pass, no re-evaluation. In the second version, status.set('Saved') updates a signal the template reads. That's one of the notifications, so Angular marks SaveButton and schedules a pass, and the label updates.

When a component can't move to signals yet, the honest fix is to send the notification yourself. markForCheck() marks the view and its ancestors and, in a zoneless app, schedules the pass:

TypeScript legacy-status.ts
import { ChangeDetectorRef, Component, inject } from '@angular/core';@Component({  selector: 'app-legacy-status',  template: `<span>{{ status }}</span>`,})export class LegacyStatus {  private readonly cdr = inject(ChangeDetectorRef);  private readonly drafts = inject(DraftApi);  status = 'Saving...';  constructor() {    this.drafts.save().then(() => {      this.status = 'Saved';      this.cdr.markForCheck(); // the notification the field can't send    });  }}

Dev mode checks your templates twice

In development, Angular runs an extra verification pass after each change detection pass and throws NG0100 (ExpressionChangedAfterItHasBeenCheckedError) if a binding's value changed in between. It's the other side of the same model: Angular renders whatever your templates return during a pass, and it expects reading them not to change anything. A getter that returns Math.random(), or a child that updates its parent's state while it's being checked, trips it. Production builds skip this check.

What your code doesDoes the view update? (Angular 22 defaults)
Assigns a plain field in a promise, setTimeout or subscribe()No: nothing notifies Angular
Sets a signal the template readsYes: the component is marked and a pass is scheduled
Runs a template listener like (click)Yes: the component and its ancestors are marked
Mutates an object or array passed to an OnPush childNo: same reference, so the child isn't marked
Calls markForCheck() after an assignmentYes: the escape hatch for code not yet on signals

How to apply it in your project

  1. Convert fields that change after async workAny field assigned after await, .then(), setTimeout or subscribe() and read by a template should become a signal. Update it with set() or update().
  2. Render Observables through the async pipeThe async pipe calls markForCheck() on every emission, so a stream rendered with it updates without extra code.
  3. Replace mutations with new valuesInstead of items.push(item), write items.update((list) => [...list, item]), so both the signal and OnPush children see a change.
  4. Keep markForCheck() for legacy codeWhere a component can't move to signals yet, call markForCheck() after every assignment that happens outside a template listener, and plan its move to signals.

Key takeaways

  • Angular doesn't watch fields: it re-evaluates templates in passes and compares results with the last render.
  • In Angular 22 apps, only notifications schedule a pass: signals read by templates, template or host listeners, markForCheck() and setInput().
  • OnPush, the default since v22, limits each pass to the components that were marked.
  • State that changes after async work belongs in a signal; markForCheck() is the fallback, not the fix.

📚 المصادر

  1. Runtime performance overview — Angular
  2. Angular without ZoneJS (Zoneless) — Angular
  3. Skipping component subtrees — Angular
  4. NG0100: Expression changed after it was checked — Angular
  5. Resolving zone pollution — Angular

التعليقات (0)