简体   繁体   English

Angular2默认更改检测

[英]Angular2 Default Change Detection

How does Change Detection works in Angular2 when changeDetection Strategy is set to "ChangeDetectionStrategy.Default"? 当changeDetection策略设置为“ChangeDetectionStrategy.Default”时,Change Detection如何在Angular2中工作? Does it check for all the bindings (by only reference) in template, and trigger re-render if any reference has been changed? 它是否检查模板中的所有绑定(仅通过引用),如果任何引用已更改,则触发重新呈现?

The default change detection strategy will run change detection for all the bindings. default change detection strategy将对所有绑定运行更改检测。 It will not only look for reference changes, but for property changes on your model as well. 它不仅会查找参考更改,还会查找模型的属性更改。

For example running the following code will change the name in the template after the changeName() method is run, even though only the name property changes, not the person reference. 例如,运行以下代码将在运行changeName()方法后更改模板中的名称,即使只更改name属性,而不是person引用。

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

@Component({
  selector: 'my-app',
  template: `
    <h1>Hello {{person.name}}</h1>
    <button (click)="changeName()">Change name!</button>
  `
})
export class AppComponent { 
  person = { name: 'Foo' };

  changeName() {
    this.person.name = 'Bar';
  } 
}

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

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