簡體   English   中英

使用@output() 進行子父通信

[英]Child to Parent Communication with @output()

我的Parent Component有一個表單,它有一個隱藏的新密碼輸入和一個編輯密碼按鈕。

Child Component通過單擊編輯密碼按鈕,將加載一個matdialog並輸入新密碼並保存。 由於這是一種不同的形式,我應該將它傳遞給Parent Component

任何人都可以幫助這個孩子與父母溝通嗎?

子組件

@Output() editedPassword = new EventEmitter<String>();
  saveButtonClick(){
    this.editedPassword.emit(this.myform.get('password').value);
  }

如何將此值傳遞給父組件?

嘗試這個:

子組件

@Output() editedEvent = new EventEmitter<String>();
  saveButtonClick(){
    this.editedEvent.emit(this.myform.get('password').value);
  } 

父 HTML

<child-app (editedEvent)="eventHandler($event)"></child-app>

父組件

public newPassword: string = '';

eventHandler($event) {
  this.newPassword = $event;
}

您還可以在此處查看完整示例。

此外,在Stackoverflow上的這個答案中有關於 Angular child-parent communication 的完整解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM