0
考慮到組件,類似於下面...父母的屬性更新正常,但孩子從未看到更改。我需要做什麼來強制孩子看到更新?隨着Angular2如何有一個基於父母的屬性的子組件更新?
//parent
@Component({
...,
// child component in parent template set to pass property
// of [obj] as the component's obj property
template: `
Child doesn't update:
<child [obj]="obj"></child>
Interpreted output doesn't either
{{obj.prop}}
`
})
export class Parent {
...
obj:Object = {original:true};
constructor() {
this._unsub = subscribeToExternalEventFeed(this.updateProp.bind(this));
}
updateProp(newObject) {
console.log('parent change', newObject);
this.obj = newObject;
}
...
}
//child @Component({ ... }) export class Child { @Input() obj:Object = {}; ngOnChange() { console.log('child change'); } }
在這個例子中,我看不到你將任何東西傳遞給孩子。那麼爲什麼你會期望它更新? –
@DavidL父母的模板在孩子上指定了[obj] =「obj」... – Tracker1
您最初並沒有在您的孩子中定義@Input。我看到你現在添加了它。 –