我正在學習有關角度更改檢測過程並檢查Chrome中的開發工具,我看到了奇怪的行爲。角度更改檢測過程重新繪製dom
我plnkr證明的行爲:http://plnkr.co/edit/cTLF00nQdhVmkHYc8IOu
我的觀點一個簡單的組件:
<li *ngFor="let item of list">{{item.name}}</li>
和構造:
constructor() {
this.list = [{name: 'Gustavo'}, {name: 'Costa'}]
模擬我添加了一個簡單的要求:
// simulating request repaint the DOM
setInterval(() => {
this.list = [{name: 'Gustavo'}, {name: 'Costa'}];
}, 2000);
如果您注意到,數組list
會收到一個等於初始值的列表。讓當角檢查值鑑於變化檢測過程中,我們有這樣的代碼來想象一下:
if(oldName !== name) { // ('Gustavo' !== 'Gustavo')
// update the view
}
但值相同,爲什麼角重繪每2秒DOM?
但如果我變異的對象,重繪沒有發生
// simulating request there is not repaint
setInterval(() => {
this.list[0].name = "Gustavo"; // no repaint because it's the same value
this.list[1].name = "Costa 2"; // repaint
}, 2000);
你可以用上面的plnkr鏈接進行測試。
這我不知道,謝謝:) – 0mpurdy
@ 0mpurdy,不客氣,感謝upvote) –
嗨馬克西姆斯,再次感謝你!對於AngularJS開發人員來說'$$ hashKey'是Angular中對象的引用? trackBy for Angular與AngularJS有相同的想法嗎? (相同的概念適用於Angular這篇文章? http://www.codelord.net/2014/04/15/improving-ng-repeat-performance-with-track-by/)? –