简体   繁体   English

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

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

I have a form in my Parent Component which has a hidden input for new password and an edit password button.我的Parent Component有一个表单,它有一个隐藏的新密码输入和一个编辑密码按钮。

In the Child Component by clicking on the edit password button, a matdialog will be loaded and to enter enter new password and save.Child Component通过单击编辑密码按钮,将加载一个matdialog并输入新密码并保存。 Since this is in a different form I should pass it to the Parent Component .由于这是一种不同的形式,我应该将它传递给Parent Component

Can anyone help for this child to parent communication?任何人都可以帮助这个孩子与父母沟通吗?

Child Component子组件

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

How can I pass this value to the parent component?如何将此值传递给父组件?

Try this:尝试这个:

Child Component子组件

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

Parent HTML父 HTML

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

Parent Component父组件

public newPassword: string = '';

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

You can also see a complete example here .您还可以在此处查看完整示例。

In addition there is a complete explanation about Angular child-parent communication in this answer on Stackoverflow .此外,在Stackoverflow上的这个答案中有关于 Angular child-parent communication 的完整解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM