2016-09-28 75 views
0

兩個組件之間的通訊,我有兩個組成部分:AppCompSimulationCompAngular2與EventEmitter

AppComp包含一個函數:

generateEmptyPromise() { 
    return Promise.resolved('') 
} 

,並具有以下HTML:

<simulation-comp (simu)='generateEmptyPromise()'></simulation-comp> 

模擬補償手柄(simu)是這樣的:

@Output() simu = new EventEmitter() 
private eventHandled: boolean = false 

// Triggered when a button of the component is pressed 
whenClicked() { 
    this.simu.subscribe(() => { 
    this.eventHandled= true 
    }) 
    this.simu.emit() 
} 

我希望eventHandled成爲真正的基礎上generateEmptyPromise給出的承諾(所以在發射後已處理)。然而,它不工作atm,我怎麼能適應我的代碼有這種行爲?也許它不應該像這樣工作,我在這裏完全錯了。

+0

的可能的複製[我可以emmit事件從父母到孩子angular2] (http://stackoverflow.com/questions/39738974/can-i-emmit-the-event-f ROM的家長對兒童在-angular2) – ranakrunal9

回答

0

@Output用於將組件的「外部」值發送回父級。孩子 - >父母。

對於父 - >子通信,其中父模板由子想與您通信需要@Input,所以孩子知道父母會在某個時間點發送這些值。

所以開始你可能想appCmp

<simulation-comp [myinput]='generateEmptyPromise()'></simulation-comp>

SimulationCmp.ts

@Input myinput : any //Whatever value type you expect it to be