DEV Community

Cover image for Angular Change Detection: The 3 Triggers (A Simplified Guide for Developers)
Md Habibur Rahman
Md Habibur Rahman

Posted on

Angular Change Detection: The 3 Triggers (A Simplified Guide for Developers)

Most Angular developers use Change Detection daily, but truly understanding when and why it runs can feel like deciphering a secret language. If you've ever wondered why your component isn't updating, or how to optimize performance, this is for you.

Let's break down Angular's Change Detection (CD) into its core triggers.

What is Change Detection?

At its heart, Change Detection is Angular's mechanism for updating the DOM (your UI) when your application's data changes. When a component's data updates, Angular needs to "detect" that change and re-render the affected parts of the view.

The 3 Core Triggers for Change Detection

Angular's CD is primarily triggered by events wrapped by Zone.js. Zone.js is a patching library that intercepts asynchronous operations in the browser and notifies Angular when they complete. This notification is what kicks off a change detection cycle.

Here are the three main categories of events that trigger a CD cycle:

1. Browser Events:
Any native DOM event that occurs in your application will trigger CD. This includes:

  • click events on buttons, links, etc.
  • input or change events in forms.
  • mousemove, scroll, keypress, etc.

When you interact with your application, Zone.js catches these events, and once the event handler finishes executing, Angular runs CD to see if any data bound to the UI has been updated.

2. Timers:
JavaScript's built-in timing functions are also patched by Zone.js. This means if you use:

  • setTimeout()
  • setInterval()
  • requestAnimationFrame()

...Angular will run a CD cycle after their respective callbacks complete. This is crucial because data often updates asynchronously within these timers.

3. HTTP Requests (XHR/Fetch):
Whenever your Angular application makes an HTTP request (e.g., to fetch data from an API using HttpClient or native fetch API), Zone.js monitors these asynchronous operations. Once the HTTP response is received and processed (e.g., data is assigned to a component property), Angular triggers a CD cycle to update the view with the new data.

The Simple Mental Model: Zone.js as the Orchestrator

Imagine Zone.js as an event listener for all asynchronous operations happening within Angular's "zone." When any of these operations (browser event, timer, HTTP request) completes, Zone.js sends a signal. Angular receives this signal and then performs a change detection cycle, checking all active components for potential data changes and updating the DOM as needed.

Optimizing Performance with ChangeDetectionStrategy.OnPush

This constant checking, while efficient, can become a performance bottleneck in large, complex applications. This is where ChangeDetectionStrategy.OnPush comes in handy.

By setting changeDetection: ChangeDetectionStrategy.OnPush in your component's decorator, you instruct Angular to run change detection for that component (and its children) only if:

  1. One of its @Input() references changes (i.e., not just a mutation within an object, but the object itself is replaced).
  2. An event originates from within the component itself (e.g., a button click inside the component).
  3. You explicitly tell Angular to run a check using ChangeDetectorRef.detectChanges() or ChangeDetectorRef.markForCheck().
  4. An observable piped through the async pipe emits a new value.

This strategy significantly reduces the number of CD cycles, leading to substantial performance improvements by making your CD more predictable and efficient.


What are your experiences with Angular Change Detection? Have you found OnPush to be a game-changer for your applications? Share your thoughts and favorite performance tips in the comments below!

#Angular #ChangeDetection #Frontend #WebDev #Performance #JavaScript

Top comments (0)