将对话框中的 md-input 值传递给父组件函数
Pass md-input value from dialog to parent component function
我在对话框 window 中有一个 md-input
字段(该对话框是子组件)。我需要将此值作为参数传递给父组件中的函数。
如何操作?
定义一个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);
Link 到 working demo.
我在对话框 window 中有一个 md-input
字段(该对话框是子组件)。我需要将此值作为参数传递给父组件中的函数。
如何操作?
定义一个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);
Link 到 working demo.