What is $scope.$apply() equivalent in Angular 6?

Viewed 6990

We had $scope.$apply() in angularjs that perform proper scope life cycle of angularjs.

Is there any equivalent to this in Angular 6?

2 Answers

You are looking for ChangeDetectorRef

Inject within your constructor

constructor(private ref: ChangeDetectorRef) {
}

and call

this.ref.detectChanges();

You can inject ChangeDetectorRef and use it for manually running change detection. It has methods that run change detection or stop it for that component. You can explore methods of ChangeDetectorRef looking above link.

import { ChangeDetectorRef } from '@angular/core';

@Component({
   ...
})
export class MyComponent {

   constructor(private changeDetector: ChangeDetectorRef ) {

   }

}
Related