2017-02-09 108 views
0

當changeDetection策略設置爲「ChangeDetectionStrategy.Default」時,更改檢測在Angular2中如何工作? 它是否檢查模板中的所有綁定(僅通過引用),並在引用已更改時觸發重新呈現?Angular2默認更改檢測

+0

看看這篇文章:https://blog.thoughtram.io/angular/2016/02/22/angular-2-change-detection-explained.html – Maxime

回答

0

default change detection strategy將運行所有綁定的變化檢測。它不僅會查找參考更改,還會查找模型上的屬性更改。

例如運行下面的代碼在運行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'; 
    } 
} 
+0

它仍然是檢查參考已在模板中綁定的屬性。如上例中「person.name」已被綁定在模板中。 –