[英]How to use EventEmitter from child component with observable in parent (angular 2)
[英]Angular 4 - How to call parent method from child component without EventEmitter?
我的應用程序中有以下層次結構:
AppComponent » ThemeComponent » HeaderNavComponent
ThemeComponent有一個方法
qrIconClicked(){
}
我需要從HeaderNavComponent調用此方法。
我怎樣才能做到這一點?
你可以通過定義@Output
在你的孩子類型的變量HeaderNavComponent
組件和氣泡事件,當你想從將由父組件捕捉孩子ThemeComponent
和火的方法qrIconClicked()
所以你的孩子組件看起來像
export class HeaderNavComponent {
@Output() editDone = new EventEmitter<EditEvent>();
onSubmit() {
if (some condition) {
this.editDone.emit('some value');
} else {
this.editDone.emit('other value');
}
}
}
當您從子組件中引發editDone
事件時,在調用子組件時,您可以分配一個函數,以便在該事件被激活時進行調用。
<header-nav(editDone)="qrIconClicked($event)"><header-nav>
最后,您的函數將在傳遞給父級的事件中從子級獲取值
qrIconClicked(event: any){
//event will hold value from chilld
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.