2017-09-08 19 views
2

我有一個表達式(模型),它是通過從日期選擇器的屬性綁定來設置的。這個表達式我想用EventEmitter發送給父組件。聆聽表達更新的最佳方式是什麼?還是有更好的方法來釋放它?發送更新的表達

@Output() date: EventEmitter<string> = new EventEmitter<string>(); 
model: string; 

謝謝!

回答

1

當你的模型值發生變化時,使用getter和setter來做你想做的任何事情。

@Output() date: EventEmitter<string> = new EventEmitter<string>(); 
_model: string; 
get model(): string { 
    return _model; 
} 
set model(value: string) { 
    this._model = value; 
    date.emit("WhateverYouWant"); 
} 
+0

哦,是的,二傳手完美的作品。謝謝 :) –

0

只要你想在EventEmitter上使用emit函數,就可以發射你的模型。這可能是最相關的,一旦你已經設置要發射模式的價值:

date.emit(model); 

然後,如果您想聽聽這個事件中,你只需做你的父組件模板如下:

<my-child-component (date)="myFunction(event)"></my-child-component> 

其中myFunction的(事件)是在你的TS文件的方法,你可以用它來訪問你的子組件發出的模式:

myFunction(model: string) { 
    whatever you want 
}