Sometimes, we want to trigger change detection manually in Angular.
In this article, we’ll look at how to trigger change detection manually in Angular.
How to trigger change detection manually in Angular?
To trigger change detection manually in Angular, we can use ChangeDetectorRef
.
For instance, we write
import { ChangeDetectorRef } from '@angular/core';
to import it.
Then in our component, we write
constructor(private ref: ChangeDetectorRef) {
}
to inject the ref
ChangeDetectorRef
object into our component.
Then in our component method, we write
this.ref.markForCheck();
to manually trigger change detection.
Conclusion
To trigger change detection manually in Angular, we can use ChangeDetectorRef
.