简体   繁体   English

了解角度2中的变化检测

[英]Understanding change detection in angular 2

I have come across below example in Angular 2 documentation 我在Angular 2文档中遇到了以下示例

@Component({
  selector: 'cmp',
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `Number of ticks: {{numberOfTicks}}`
})
class Cmp {
  numberOfTicks = 0;
  constructor(ref: ChangeDetectorRef) {
    setInterval(() => {
      this.numberOfTicks ++
     // the following is required, otherwise the view will not be updated
     this.ref.markForCheck();
    }, 1000);
  }
}

As mentioned above , when changeDetection is to ChangeDetectionStrategy.OnPush , the view gets updated only when "this.ref.markForCheck();" 如上所述,当changeDetection更改为ChangeDetectionStrategy.OnPush时,仅在“ this.ref.markForCheck();”时更新视图。 is invoked. 被调用。

Can any one please explain the importance of markForCheck() method here. 任何人都可以在这里解释markForCheck()方法的重要性。

With ChangeDetectionStrategy.OnPush Angular runs change detection, when in @Input() was updated, a DOM event Angular listens to was received, or the async pipe ( | async ) received a new value. 使用ChangeDetectionStrategy.OnPush Angular运行更改检测,当在@Input()中更新时,接收到Angular侦听的DOM事件,或者异步管道( | async )接收了一个新值。

If you for example subscribe to an observable from a service and update the status of the component, bindings to this status won't be updated, because this is not covered by above list. 例如,如果您从服务订阅了一个可观察对象并更新了组件的状态,则不会更新对此状态的绑定,因为上面的列表未涵盖此绑定。 If you call this.ref.markForCheck() you tell Angular that it should run change detection because there actually are changes that need to be updated (that's also what the async pipe does). 如果调用this.ref.markForCheck() ,则告诉Angular它应该运行更改检测,因为实际上存在需要更新的更改(这也是异步管道的功能)。

Other cases are if you explicitly ( this.zone.runOutsideAngular() ) or for some other reasons code runs outside Angulars zone modifies the status of the component, this also won't be covered (even when the code is an event handler). 其他情况是,如果您显式( this.zone.runOutsideAngular() )或出于某些其他原因,在Angulars区域外运行的代码修改了组件的状态,则也不会涉及(即使代码是事件处理程序)。

Using ChangeDetectionStrategy.OnPush tells Angular not to perform change detection on your component (ie updating its view) unless one or more of the component's inputs has changed (these inputs should be immutable objects). 使用ChangeDetectionStrategy.OnPush告诉Angular不要对组件执行更改检测(即更新其视图),除非组件的一个或多个输入更改(这些输入应该是不可变的对象)。

For any events that come from within the component itself and require the view to be updated, you have to explicitly tell the change detector to look for changes in that component on its next change detection run. 对于来自组件本身内部且需要更新视图的任何事件,您必须明确告知更改检测器在其下一次更改检测运行中查找该组件中的更改。

In this snippet, ref is a reference to the change detector. 在此代码段中, ref是更改检测器的参考。 Calling ref.markForCheck() tells the change detector that something has happened that will change the view (ie numberOfTicks has been incremented) and it needs to recompute it. 调用ref.markForCheck()通知变更检测器发生了一些会改变视图的事情(即numberOfTicks已增加),需要重新计算。

This is not my content. 这不是我的内容。 But, I think, the best explanation related to the above question is provided into below link: 但是,我认为,以下链接提供了与上述问题相关的最佳解释:

Understanding Change Detection Strategy in Angular 了解Angular中的变更检测策略

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM