← Back to Angular, page 6

angular-0119 · Medium mid

What happens inside Angular from the moment a user clicks a button until the screen updates, with zone.js and without it?

With zone.js, the browser event runs inside the Angular zone, the handler updates state, and when the zone becomes stable it calls ApplicationRef.tick, which checks the views from the root. Without zone.js, a signal change or markForCheck notifies the scheduler, which runs tick in the next task. Either way, tick refreshes templates and updates the DOM.

Full answer

Walk through it step by step, and give both models.

  1. The user clicks. Angular registered the event listener when it created the view. With zone.js, addEventListener was patched, so the handler runs inside the Angular zone (NgZone).
  2. Angular's listener wrapper runs your handler, for example count.update(...). For OnPush views, the wrapper also marks the view and its ancestors dirty.
  3. Zone.js tracks tasks. When the handler and any microtasks finish and no more are pending, the zone emits onMicrotaskEmpty. NgZone then tells ApplicationRef to run tick().
  4. ApplicationRef.tick() walks the view tree from the root. For each dirty (or default strategy) view it runs the template function in update mode, which compares bound values with the previous ones and updates the DOM only where they differ. It also calls ngDoCheck, content and view hooks, and runs effects and afterRender callbacks. In development mode it then runs a second checkNoChanges pass.
  5. The browser paints.

Without zone.js (zoneless, stable in 20.2): nothing patches events, so Angular needs a notification. Sources are: signals read in a template, markForCheck, the async pipe, ComponentRef.setInput, template event listeners, and attaching views. These call the change detection scheduler, which schedules a tick() in the next macrotask (or animation frame, whichever comes first). The rest of step 4 is identical.

Important consequences:

  • With zone.js any async work in the zone (timers, promises, XHR) triggers a pass, even if nothing changed. That is simple but can be wasteful.
  • With signals and zoneless, only views that read a changed signal are refreshed.
  • A change made outside the zone (runOutsideAngular, or a callback from a library that zone.js did not patch) does not trigger a pass in the zone model.
  • You can call appRef.tick() yourself, but you rarely should.
typescript
import { ApplicationRef, Component, DoCheck, inject, signal } from '@angular/core';

@Component({
  selector: 'app-click-flow',
  template: `<button (click)="increment()">Clicked {{ count() }}</button>`,
})
export class ClickFlowComponent implements DoCheck {
  private readonly appRef = inject(ApplicationRef);
  readonly count = signal(0);
  checks = 0;

  ngDoCheck(): void {
    this.checks++; // runs once per change detection pass of this view
  }

  increment(): void {
    this.count.update((c) => c + 1);
  }

  forceTick(): void {
    this.appRef.tick(); // runs a full pass now. Rarely needed.
  }
}

Angular 20.3, type-checked. The component counts its `ngDoCheck` calls. A Karma test checks that a click updates the DOM and increases the count, and calls `ApplicationRef.tick()` once.

Key points

  • Event listener runs in the Angular zone, handler changes state.
  • Zone stability triggers `ApplicationRef.tick()`, which refreshes views from the root.
  • Zoneless: signals, `markForCheck`, events and the async pipe notify a scheduler that runs `tick`.
  • Template functions update only changed bindings. Dev mode adds a `checkNoChanges` pass.

Common mistakes

  • Believing Angular re-renders the whole page on every event.
  • Updating state outside the zone and expecting the screen to change.
  • Calling `tick()` manually to hide a data-flow problem.

Follow-ups

  • What does `runOutsideAngular` change in this flow?
  • Why does a plain object mutation not update an OnPush component?
  • What is the second `checkNoChanges` pass for?

Tips

  • Number the steps. It shows you know the pipeline.
  • Give both models and say what replaces zone.js.
  • Mention that only changed bindings touch the DOM.

Evaluation rubric

  • Describes event, handler, tick, template update, and DOM update.
  • Explains the zoneless alternative correctly.
  • Understands consequences for performance.