I'm learning about Angular change detection process and I'm checking the Chrome dev tools, I see strange behavior.
My plnkr to demonstrate the behavior: http://plnkr.co/edit/cTLF00nQdhVmkHYc8IOu
I have a simple component view:
<li *ngFor="let item of list">{{item.name}}</li>
and the its constructor:
constructor() {
this.list = [{name: 'Gustavo'}, {name: 'Costa'}]
to simulate a simple request I've added:
// simulating request repaint the DOM
setInterval( () => {
this.list = [{name: 'Gustavo'}, {name: 'Costa'}];
}, 2000);
If you noticed, the array list
receives a list equal to the initial value. Let's imagine when Angular checks the values in view in change detection process we have a code like this:
if( oldName !== name ) { // ( 'Gustavo' !== 'Gustavo')
// update the view
}
But the values are the same, why angular REPAINT THE DOM every 2 seconds.?
But if I mutate the object, the REPAINT does not occur
// 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);
You can test this with the plnkr link above.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…