Angular Change Detection – Part 1: Checking Views

Posted by


Change detection is a crucial concept in Angular applications as it helps in detecting changes in the application state and updating the view accordingly. In this tutorial, we will discuss the various ways of performing change detection in Angular, starting with view checking.

View checking is the default change detection mechanism used by Angular to detect changes in the components’ properties. When a component’s properties are changed, Angular checks the view associated with that component to see if any changes need to be reflected in the view.

Here’s how view checking works in Angular:

  1. Angular’s Change Detection Cycle: Angular follows a change detection cycle to detect changes in the application state. When an event occurs in the application, such as a user input or a timer event, Angular triggers change detection to check for any changes in the application state.

  2. Component Properties: Each component in an Angular application has properties that define the state of the component. When these properties are changed, Angular triggers change detection to update the view.

  3. Change Detection Strategy: Angular provides two change detection strategies – Default and OnPush. The default strategy uses view checking to detect changes in the components’ properties, while the OnPush strategy uses reference checking to detect changes.

  4. Automatic Change Detection: By default, Angular automatically performs change detection for all components in the application. This means that whenever a component’s properties are changed, Angular automatically updates the view associated with that component.

  5. Zone.js: Angular uses Zone.js to intercept asynchronous events in the application and trigger change detection. Zone.js helps in detecting changes caused by asynchronous events such as timer events, XHR requests, and DOM events.

To demonstrate view checking in Angular, let’s create a simple component that displays a counter and updates the view whenever the counter value is changed. Here’s the code for the component:

import { Component } from '@angular/core';

@Component({
  selector: 'app-counter',
  template: `
    <h2>Counter: {{ counter }}</h2>
    <button (click)="increment()">Increment</button>
  `
})
export class CounterComponent {
  counter: number = 0;

  increment() {
    this.counter++;
  }
}

In this component, we have a counter property that stores the current value of the counter. We also have an increment method that increments the counter value whenever the button is clicked.

Now, let’s add this component to the app module and run the application:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { CounterComponent } from './counter.component';

@NgModule({
  declarations: [
    AppComponent,
    CounterComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

In the app component HTML file, we can add the counter component:

<app-counter></app-counter>

Now, when you run the application and click the "Increment" button, you will see that the counter value is updated in the view. This is because Angular is using view checking to detect changes in the counter property and update the view accordingly.

In the next part of this tutorial, we will discuss the OnPush change detection strategy and how it differs from view checking. Stay tuned for more on Angular change detection!

0 0 votes
Article Rating
41 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@IhorKhomiak
1 month ago

So good explanation of the Change Detection mechanism!

@carlosandrescarrasquilla3838
1 month ago

I just want to say, i love you ❤

@user-cx7wz9mm1q
1 month ago

Watching all of your videos. You have improved my career so much bro, many thanks…🙏

@criticalthinker1123
1 month ago

why don't you create Angular under the hood and break down everything like this. like the course for Tony JavaScript the weird parts

I'm sure many will appreciate and purchase such course

@VK-sn3sn
1 month ago

Hello, what are your views about Angular 17 vs React? Which one is more better taking general things into consideration.

@oren-karaso
1 month ago

Such an amazing professional, thanks for enriching the Angular world and making complex content accessible!

@4444-c4s
1 month ago

Hello,
I have a large formGroup in parent component and passed to multiple nested child components. So can this hamper performance?? As currently it takes more than 3 seconds after click just to show its UI on page.

When I load the parent's page, the logic is such that it navigates to default child route, and in that child, it again navigate to another child causing 3 layers of routing.
I have ised this pattern from 1 year now since the project started. Now it is impossible to revert it.

@mohamedbadr267
1 month ago

I want to thank you for the detailed explanation
And I have a small question
When I navigate through the core code I found it written as mjs, so I want to ask you how can I open the TS files like video?!

Thanks again

@rushimore9635
1 month ago

hello i want to buy your complete angular course and your advanced angular form concept is included in that course.please respond can i get discount in that full course please provide your email i so can communicate with you

@halynaua
1 month ago

Thank you, Dmytro!

@USONOFAV
1 month ago

Is there like a way to invoke change detection? Sometime some event are not happening because of timing issue, so to resolve the issue I use setTimeout to deferred setting of a value the would invoke an event.

@Vgogichashvili
1 month ago

So, the the automatic view checking still happens, after using 'noop', but happens only once, during initialization?

@Anton-qw8mh
1 month ago

Amazing, thanks

@AmarSingh-uw1db
1 month ago

Gratitude ❤❤

@rashidanwar101
1 month ago

Concepts Indepth❤
Love from India..

@mustafakaladagi2914
1 month ago

great content.

@sanketjain7788
1 month ago

Amazing content! Thank you so much

@syedahmedali8004
1 month ago

Really love your work, Dmytro. may I ask where I can get the internal code of angular for learning purposes? thanks

@sanjaygarg3540
1 month ago

Recently took your Angular Forms Course, It was just amazing, learned a lot of cool stuff, This one is also amazing !

@MrVitalirapalis
1 month ago

Very clear explanation, thx.