0
我在对话框窗口(该对话框是一个子组件)内有一个md-input
字段。我需要将此值作为父组件中的函数的参数传递。 如何做?将对话框中的md输入值传递给父组件函数
我在对话框窗口(该对话框是一个子组件)内有一个md-input
字段。我需要将此值作为父组件中的函数的参数传递。 如何做?将对话框中的md输入值传递给父组件函数
定义shared.service
其中输入值传递给父组件:
import {Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable()
export class SharedService{
public triggerParentMethod: Subject<string> = new Subject<string>();
}
在你ParentComponent
,订阅triggerParentMethod
在构造:
constructor(private sharedService:SharedService,public dialog: MdDialog){
this.sharedService.triggerParentMethod.subscribe(someValueFromDialog =>{
// Pass the value to the method here.
this.someMethod(someValueFromDialog);
});
}
绑定您<md-input>
一些[(ngModule)]
:
您可以发出从你的对话框,输入值是这样的:
this.sharedService.triggerParentMethod.next(this.someField);
链接working demo。
https://angular.io/guide/component-interaction#component-interaction – porgo
您是否测试过我提供的解决方案? – Faisal