Angular Change Detection – How It Works?

Thursday, April 17, 2025

Are you often overwhelmed by the need to manually update the changes in your Angular application? What if your app automatically handles updates whenever it detects changes? This is not a mere concept but a reality with Angular change detection. It’s a built-in feature of the Angular framework that addresses many development problems. Angular development companies utilize it to find bugs and optimize their application. They can even simplify the management of complex datasets and enhance app efficiency. 

With so many benefits, it’s essential to explore this feature rather than accept it at face value. This blog explains the functionalities of change detection in the Angular framework and discusses strategies for its effective implementation.

1. What Is Change Detection in Angular?

Change detection is an Angular-specific feature that monitors and identifies the changes in your app components. The newly updated values or changes in the objects are immediately displayed to the end-users by re-rendering the view. 

The Angular change detection mechanism runs every time an event takes place in your app, such as user interactions or asynchronous operations. It checks if there are any changes in the app’s state. This way, the Angular framework ensures that the UI is in sync with the internal state of the application, effectively keeping the view and components aligned.

2. How is Change Detection Implemented?

Change detection in Angular is a mechanism that analyzes changes in the state of the components or associated DOM elements. Its process involves comparing the current value of the variables and properties with their previous values. 

Some common reasons why changes happen in the state of the application are as follows: 

  • User interactions include keyboard navigation, scrolling, and mouse clicks. 
  • Utilization of setInterval, setTimeout, and other JavaScript timer functions. 
  • Accumulated data from component events, such as user actions or emitted events and contributes to state changes. 
  • Asynchronous operations like AJAX calls. 

3. How Angular Detects Changes?

Because of the reasons we discussed before, if something changes in your app, Angular performs a change detection on all the components from top to bottom. The framework offers a change detector for every component. It can read the binding on the Angular template and reflect the changed data to the view. This way, both the data model and DOM are kept in sync.

Let’s say you want to update the data model through a component binding. Any changes you make in the component template will be detected by the change detectors. The framework will then run a check on all the components to identify and verify the changes. Once the new value is found, it is immediately updated to the DOM.

In case a user, while filling an online form, clicks on the change address button. The click of that button triggers change detection for each View in the change decision tree. After checking all the views for changes, the change detector would update the value of the user’s first name property, which was initially requested to be changed.

4. Angular Change Detection Strategies

You need a suitable strategy for effective Angular change detection. Mainly, there are two ways about it. Consider your requirements to pick the right fit for you.

Strategy 1: Default Change Detection Strategy

When you create a component, you have to specify a change detection strategy for it. If you don’t, then it uses the default change detection strategy, i.e., CheckAlways. In this strategy, Angular carries out a process called dirty checking, where it compares the current values of DOM elements and component state with their previous values. 

These check runs are run frequently, even if no changes are made to the application’s data model. Angular will check the entire component tree for changes. It analyzes every component’s template expressions that include event bindings and property bindings. If Angular detects any change, it is instantly updated in the View. 

This strategy bears fruit only if any changes occur. If not, then the entire process becomes a waste of time. A default strategy can also start an unnecessary change detection cycle in a large and complex application. Running such inessential application-wide checks can have a negative impact on the app’s performance.

Strategy 2: OnPush Strategy

OnPush strategy can speed up the change detection process in Angular. By taking reference types as immutable objects, every time the value changes, the reference in the stack memory is also updated. 

The only thing left to check then would be the reference of the reference type. If it has indeed changed, then you have to check out all the values.

So, the OnPush strategy asks two questions: 

  1. Has the reference type changed? 
  2. If yes, then have the values in the heap memory changed? 

Let’s assume your app consists of an immutable array of 30 elements. Now, we know that if the immutable array is updated, then its references would have changed as well. So, when you seek to determine if there have been any changes to the immutable array, simply check whether the reference of the array is different. 

If it is, then you will be saved from running a different check on every single element. But here, in a single check, you can determine which element has seen a change in its value. This is how OnPush change detection strategy works.  

Now, a question may arise in some people’s mind: What’s the meaning of treating the reference types as immutable in OnPush change detection? Well, when you treat the reference types as immutable objects, you never really set a property for the reference type. Instead, you just reassign all the values. See the code examples below to understand it properly. 

When you treat the objects as mutable:

static mutable() {
  var a = {name: "jeremy"};
  var b = a;
  a.name = "tom";
  console.log(a === b);
  // => true
}

When you treat the objects as immutable:

static mutable() {
  var a = {name: "jeremy"};
  var b = a;
  a = {name: "tom"};
  console.log(a === b);
  // => false
}

It is important to mention that, in the above example, we are treating the reference types as immutable objects only by convention. Therefore, in reality, we are still working with mutable objects. But when implementing the OnPush change detection strategy, we have to pretend that they are immutable. 

But how do we execute the OnPush change detection strategy for an Angular component? It’s easy actually. Just add the changeDetection parameter in their @Component annotation.

import {ChangeDetectionStrategy, Component} from '@angular/core';
@Component({
  // ...
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class OnPushComponent {
  // ...
}

5. Summary

Change detection is very helpful in delivering a seamless user experience and Angular app performance. There are two ways to do it: default and OnPush change detection strategy. 

It is convenient to use the default or AlwaysCheck strategy. But it can also be resource-intensive and give way to performance issues. Meanwhile, OnPush change detection saves you from potential pitfalls. And helps you embrace immutability with input reference detection. 

Understand the capabilities of Angular change detection, effectively implement a suitable strategy, and you can create high-performing and responsive Angular applications.

FAQs

What is Angular change detection?

Angular change detection is a built-in feature specific to the Angular framework. It helps ensure that a component’s data and its HTML template View are synchronized automatically. This way, any changes in the value of the component will be instantly reflected in the View. 

How to detect changes in Angular?

Angular has a default mechanism that frequently conducts change detection to ensure that changes in the data model are reflected in the app’s view. Any asynchronous event can trigger the change detection process. You can also do it manually.

Comments


Your comment is awaiting moderation.