How to trigger change detection manually in Angular

Imgur

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();

    }

}

Reactions

Post a Comment

0 Comments

Close Menu