在Angular 6中等效的$ scope。$ apply()是什么?

Moh*_*deh 2 angularjs angularjs-scope angular angular6

我们$scope.$apply()在angularjs中执行了正确的angularjs范围生命周期。

Angular 6中有与此等效的东西吗?

Saj*_*ran 9

你在找 ChangeDetectorRef

在构造函数中注入

constructor(private ref: ChangeDetectorRef) {
}
Run Code Online (Sandbox Code Playgroud)

并打电话

this.ref.detectChanges();
Run Code Online (Sandbox Code Playgroud)


Sur*_*yan 5

您可以注入ChangeDetectorRef并将其用于手动运行更改检测。它具有为该组件运行更改检测或停止它的方法。您可以探索ChangeDetectorRef查看上述链接的方法。

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

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

   constructor(private changeDetector: ChangeDetectorRef ) {

   }

}
Run Code Online (Sandbox Code Playgroud)