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:
-
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.
-
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.
-
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.
-
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.
- 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!
So good explanation of the Change Detection mechanism!
I just want to say, i love you ❤
Watching all of your videos. You have improved my career so much bro, many thanks…🙏
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
Hello, what are your views about Angular 17 vs React? Which one is more better taking general things into consideration.
Such an amazing professional, thanks for enriching the Angular world and making complex content accessible!
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.
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
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
Thank you, Dmytro!
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.
So, the the automatic view checking still happens, after using 'noop', but happens only once, during initialization?
Amazing, thanks
Gratitude ❤❤
Concepts Indepth❤
Love from India..
great content.
Amazing content! Thank you so much
Really love your work, Dmytro. may I ask where I can get the internal code of angular for learning purposes? thanks
Recently took your Angular Forms Course, It was just amazing, learned a lot of cool stuff, This one is also amazing !
Very clear explanation, thx.