und*_*ned 5 angular2-changedetection angular
我正在阅读这篇文章,其中有一节介绍何时使用markForChange().
在他的示例中,他具有以下组件:
@Component({
selector: 'cart-badge',
template: '{{counter}}',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class CartBadgeComponent {
@Input() addItemStream;
counter = 0;
constructor(private cd: ChangeDetectorRef) {}
ngOnInit() {
this.addItemStream.subscribe(() => {
this.counter++;
this.cd.markForCheck();
});
}
}
Run Code Online (Sandbox Code Playgroud)
他使用该markForCheck()方法来更新视图。我不明白的是,为什么markForCheck()当调用detectChanges() 也更新视图时我们需要在这里使用?
我在 StackOverflow 上阅读了问题的答案 - MarkForCheck() 和 detectorChanges() 有什么区别?
但不符合上面的例子。那么,为什么不打电话detectChanges()呢markForCheck()?
小智 0
markForCheck将向父节点(根)发送该组件想要检查更新视图的信息。并且组件的子组件可能不会更新。
detectChanges将检查更新组件及其子组件的视图。所以这种方式需要比markForCheck
| 归档时间: |
|
| 查看次数: |
3843 次 |
| 最近记录: |