Say we have component A as the parent component and component B as the child component. The child has an input what_is_x that is supplied by the parent. Something like this:
For the parent:
@Component({
selector: 'cmp-A',
directives: [ComponentB],
template: `<cmp-B [what-is-x]="x"></cmp-B>`
})
export ComponentA {
public x: any = [1, 2, 3];
constructor() {
setTimeout(() => this.x.push(10), 2000);
}
}
For the child:
// component B with input what_is_x
@Component({
selector: 'cmp-B',
template: `{{what_is_x}}`
})
export ComponentB {
@Input('what-is-x') public what_is_x: any;
}
My question is, if the parent changed x by some means, does the child get updated with the new value? According to the "Component communication" chapter in the developer guide (not released yet); it should! but it's not updated for me, tried all betas up till now (0,1,2)
pushdoesn't return a new array. The Input will be updated when the change detector that the value bound to it has a new reference. See this plnkr using concat that returns a new array, therefore a new reference.<div *ngFor="#item of what_is_x">{{item}}</div>. This works (using the same array reference) because bindings are created for each element of the array, so change detection will notice if an item is added or removed from the array, with, e.g.,push().