$scope.$apply 在 Angular5 中等效



我正在做一个项目(Ionic3和Angular5(。

我已从外部.js文件中更改了以下输入中的值

<input [(ngModel)]="name" 
(ngModelChange)="valueChange()" 
id="data">

但是不调用函数valueChange()

我以前在 Angular1 中使用$scope.$apply这样做过,但在Angular5中我不知道如何做同样的事情。在Angular5中执行此操作的等效方法是什么?

markForCheck():将所有要检查的 ChangeDetectionStrategy 祖先标记为要检查。detectChanges():检查更改检测器及其子项。

例:-

import { Component,
Input,
ChangeDetectionStrategy,
ChangeDetectorRef } from '@angular/core';
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ChildComponent {
@Input() data: string[];
constructor(private cd: ChangeDetectorRef) {}
refresh() {
this.cd.detectChanges();
}
}

这是关于角度变化检测的非常好的文章:- https://alligator.io/angular/change-detection-strategy/

最新更新