Categories
Angular Answers

How to trigger change detection manually in Angular?

Spread the love

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 .

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *