How to trigger change detection manually in Angular


How to trigger change detection manually in Angular

If you want to trigger the Angular change detection manually, then you can use ChangeDectorRef class. You can inject the ChangeDectorRef in your component/service, and then you can call detectChanges instance method

import { Component, ChangeDetectorRef } from "@angular/core";


@Component({

    selector: "my-app",
    templateUrl: "./app.component.html",
    styleUrls: ["./app.component.css"]

})

export class AppComponent {
    constructor(private cd: ChangeDetectorRef) { }
    doSomething() {

        // Perform some logic
        this.cd.detectChanges();

    }

}

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post