Consider this plunker
Note: To observe the effect you have to rerun the app after the entering the link
import {Component, OnInit, Input, OnChanges, DoCheck} from 'angular2/core'
@Component({
selector: 'sub',
template: `
<li *ngFor="#elem of sub_list">
<div>{{elem['name']}}</div>
</li>
`
})
export class Sub {
@Input()
sub_list: Object[];
ngOnInit(){
console.log('init');
console.log(this.sub_list);
}
ngOnChanges(){
console.log('change');
console.log(this.sub_list);
}
ngDoCheck() {
console.log('check');
console.log(this.sub_list);
}
}
@Component({
selector: 'my-app',
template: `
<div>
<sub
[sub_list]="my_list"
>
</sub>
</div>
`,
directives: [Sub]
})
export class App {
my_list: Object[] = [];
ngOnInit() {
var vm = this;
setTimeout(function() {
for(var i = 0; i < 100; i++) {
vm.my_list.push({
'index' : i,
'name' : i
});
}
}, 100);
}
}
If I try to print out this.sub_list
in Sub
's ngOnChange
the browser outputs an empty list.
However we can see that ngDoCheck
still correctly captures the change.
Is there a specific reason for this?