Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Is there any equivalent to this in Angular 6?

like image 300
Mohamad Shiralizadeh Avatar asked Aug 25 '18 04:08

Mohamad Shiralizadeh


2 Answers

You are looking for ChangeDetectorRef

Inject within your constructor

constructor(private ref: ChangeDetectorRef) {
}

and call

this.ref.detectChanges();
like image 58
Sajeetharan Avatar answered Nov 08 '22 12:11

Sajeetharan


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 ) {

   }

}
like image 32
Suren Srapyan Avatar answered Nov 08 '22 13:11

Suren Srapyan