2016-03-14 43 views
0

考慮到組件,類似於下面...父母的屬性更新正常,但孩子從未看到更改。我需要做什麼來強制孩子看到更新?隨着Angular2如何有一個基於父母的屬性的子組件更新?

[email protected]

 
//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'); } }

+0

在這個例子中,我看不到你將任何東西傳遞給孩子。那麼爲什麼你會期望它更新? –

+0

@DavidL父母的模板在孩子上指定了[obj] =「obj」... – Tracker1

+0

您最初並沒有在您的孩子中定義@Input。我看到你現在添加了它。 –

回答

1

很顯然,我沒有被正確加載全局/ polyfills ...

BEFORE:破碎

import 'babel-polyfill'; 
import 'zone.js/dist/zone'; 
import 'reflect-metadata';


AFTER:Working

import 'babel-polyfill'; 
import 'reflect-metadata'; 
import 'zone.js/dist/zone-microtask';

相關問題