Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
disable change detection anagular

How to disable automatic change detection strategy in angular component?

Aman Jain, July 4, 2021November 8, 2023

In angular all components by default take the changes as any event occur, but sometime we disable automatic change detection strategy in angular. Thus, to detach the two way binding in component we can use changeDetection strategy in component metadata. Angular Change Detection Strategy is simple to implement using the metadata of component.

There is two type of change detection strategies to disable automatic change detection strategy in angular.

  1. ChangeDetectionStrategy.Default
  2. ChangeDetectionStrategy.onPush

ChangeDetectionStrategy.Default

By default angular use ChangeDetectionStrategy.Default which means if you make changes in any variable, event, promises or XHR request then it will make all changes in your html file as well.

@Component({
  selector: "app-root",
  template: `
    <div>
      <h1>Counter: {{ this.inc }}</h1>
      <input
        type="button"
        (click)="this.update()"
        value="Update Counter"
      />
      <child-component></child-component>
    </div>
  `
})
export class AppComponent {
  inc = 0;

  update() {
    this.inc += 1;
  }
}



The above example will also update the child content.

ChangeDetectionStrategy.onPush

If we want more performance in our application or do not want dirty check on each event, XHR etc. then we can use ChangeDetectionStrategy.onPush. you basically tell angular to not guess anything on any event, it will only reflect the template if @Input decorator value changed or if developer wanted to make changes using the componentRef.markForChange() or changeDetectionRef.detectChanges() method.

@Component({
  selector: "app-root",
  template: `
    <div>
      <h1>Counter: {{ this.inc }}</h1>
      <input
        type="button"
        (click)="this.update()"
        value="Update Counter"
      />
      <child-component></child-component>
    </div>
  `,
changeDetection:ChangeDetectionStrategy.OnPush
})
export class AppComponent {
  inc = 0;

  update() {
    this.inc += 1;
  }
}



What is ChangeDetectionRef and how to use it?

ChangeDetectionRef is a base class which provides change detection functionality, when we called this ChangeDetectionRef.detectChanges() forcefully, it means detectChanges check the view and childrens and re-render the view even if the changeDetection strategy in component is ChangeDetectionStrategy.OnPush.

See the example below:

Related

Softwares Angular Javascript Ubuntu

Post navigation

Previous post
Next post

Related Posts

Javascript Directives in angular

What is directives and types of directives in angular?

September 1, 2021February 8, 2024

Directives in angular are custom tags and custom attributes of html which is controlled by class and decorators. Using directives we can change the behaviour, style and logic of a specific area. Example: helloworld.component.ts Xyz.compenent.html <hello-world></hello-world> Output:Hello world component/directive Types of Directives in Angular: 1. Component : Angular page is…

Read More

How to generate component in angular cli?

August 29, 2021August 29, 2021

Angular cli has rich features of generating components, providers, modules and many more. Angular cli can also be used for build production ready product and serve the project for local environment. Usage of Angular cli ng generate command To generate the component ng generate component component_name To generate the modules…

Read More
Javascript Angular If Else

How to use NgIf, Ngif else and elseif in angular 12 ?

April 2, 2022November 7, 2023

In this tutorial we will learn to use angular if else, ngIf, ngif else and elseif. if else is used to make conditional statement in template same as we can also use if else. As we all programmers know if or if else condition is used to create conditional statements…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version