简体   繁体   中英

Angular2 Default Change Detection

How does Change Detection works in Angular2 when changeDetection Strategy is set to "ChangeDetectionStrategy.Default"? 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. 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.

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';
  } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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