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

Zoneless Angular: What Schedules Rendering Without zone.js

zone.js only told Angular when to check. Without it, your signals and listeners do, and Angular schedules the pass.

ReadIn

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

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

A team moves its chat app to zoneless. New messages stop appearing, so the fix that ships is cdr.detectChanges() after every assignment, forty of them across the codebase. The inbox works again. Then two bug reports arrive: the unread badge in the header is stuck, and after a reconnect that replays 200 messages, the tab stutters while the inbox renders 200 times in a row.

The common misconception

The belief: zone.js is what makes Angular update the UI. Without it, Angular can't know when to render, so going zoneless means telling it yourself with detectChanges() wherever state changes.

For years the two were taught as one thing. When a third-party callback didn't update the view, the standard answer was NgZone.run(), and the next most popular answer was detectChanges(). Both suggest that zone.js is the engine and that, without the engine, you push the car yourself. The half-truth is real: in a zoneless app, something has to tell Angular that state changed. But your code already sends those signals, often literally, and Angular schedules the render itself.

What actually happens

zone.js was a trigger, never the detector

zone.js patches the browser's asynchronous APIs: event listeners, timers, promises, network requests. When a task that ran inside Angular's zone finished, Angular ran for the whole app. zone.js had no idea whether anything had changed, and Angular's zoneless guide puts that first among the reasons to drop it: change detection runs far more often than necessary. The detection itself, walking the component tree and comparing each binding with its last value, was always Angular's own code. The API docs describe the switch in one line: zone.js uses browser events to trigger change detection, and Angular uses Angular APIs to schedule it instead.

Notifications replace the guess

Without zone.js, Angular schedules a pass when its own APIs tell it something changed: a signal read by a template is set, a template or host listener runs, markForCheck() is called (the async pipe does it for you), setInput() sets an input, or a dirty view is attached. The API reference adds two more: removing a view and registering a render hook. The first notification schedules one pass for shortly after the current task, and every notification that arrives before it runs joins that same pass. Ten signal writes in one callback cost one render, not ten.

Why detectChanges() is the wrong translation

detectChanges() isn't a notification; it's a check that runs synchronously, right now, on one view and its children. That explains both bug reports. Each replayed message re-rendered the inbox on the spot, 200 renders in one task where one would do. And the header isn't a child of the inbox, so the badge it shows was never checked. The calls also leave the cause in place: state that Angular can't see change, so the next developer who assigns a field and forgets the call brings the bug back. Angular's docs pair detectChanges() with detach(), for views you deliberately check by hand.

TypeScript inbox.ts
import { ChangeDetectorRef, Component, ElementRef } from '@angular/core';import { inject, viewChild } from '@angular/core';@Component({ selector: 'app-inbox', templateUrl: './inbox.html' })export class Inbox {  private readonly cdr = inject(ChangeDetectorRef);  private readonly list = viewChild.required<ElementRef<HTMLElement>>('list');  messages: Message[] = [];  constructor() {    inject(ChatSocket).messages$.subscribe((m) => {      this.messages = [...this.messages, m];      this.cdr.detectChanges(); // renders Inbox right now, once per message      this.list().nativeElement.lastElementChild?.scrollIntoView();    });  }}

In the fixed version, each message sets a signal. That's a notification, so Angular schedules a pass, and a burst of messages shares it. The scroll moves into afterRenderEffect(), which runs after Angular has updated the DOM and re-runs whenever a signal it read changed. The wrong version only found the new <li> because it forced a render first. Any badge that reads the same state through a signal updates too, wherever it sits in the tree.

Waiting for a render without onStable

Zone-era code often waited for "Angular is done" with NgZone.onStable or onMicrotaskEmpty. In a zoneless app those Observables never emit, and NgZone.isStable is always true, so that code silently stops working. The replacements are the : afterNextRender() for one render, like initializing a chart library, and afterEveryRender() for work that spans several. Prefer an explicit phase, such as write or read, because a bare callback runs in mixedReadWrite, which the docs warn can hurt performance. NgZone.run() and runOutsideAngular() can stay: they're compatible with zoneless, and removing them from a library can slow down apps that still use zone.js.

Zone-era habitZoneless replacement
Assign a field in a callback and let zone.js renderSet a signal, or call markForCheck() in legacy code
detectChanges() after each changeNothing: the notification schedules the pass
NgZone.onStable or onMicrotaskEmpty to wait for a renderafterNextRender() or afterEveryRender()
SSR that waits for zone stabilityPendingTasks.run() around async work that must finish first
fixture.detectChanges() in testsawait fixture.whenStable()

How to apply it in your project

  1. Confirm the app is really zonelessMake sure provideZoneChangeDetection() isn't overriding the default, then remove zone.js and zone.js/testing from the polyfills in angular.json and uninstall the package.
  2. Turn forced renders into notificationsSearch for detectChanges(). Where it follows an assignment, make the state a signal and delete the call. Keep it only for views you detached on purpose.
  3. Replace zone-stability codeSwap onStable and onMicrotaskEmpty subscriptions for afterNextRender() or afterEveryRender(), and delete isStable checks.
  4. Register async work for SSRWrap async work that must finish before the server serializes the page in PendingTasks.run(). HttpClient requests and router navigations are already tracked.

Key takeaways

  • zone.js never detected changes; it told Angular when to run change detection, after every async task.
  • Zoneless Angular runs the same change detection, scheduled by notifications: signals, listeners, markForCheck() and setInput().
  • detectChanges() renders one subtree synchronously; a notification schedules one pass for everything that changed.
  • Replace zone-stability hooks with afterNextRender(), afterEveryRender() and PendingTasks.

📚 المصادر

  1. Angular without ZoneJS (Zoneless) — Angular
  2. afterNextRender — Angular API
  3. provideZonelessChangeDetection — Angular API
  4. ChangeDetectorRef — Angular API
  5. Effects: side effects on DOM elements — Angular

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