从 md-button Angular Material 触发另一个按钮点击
Trigger another button click from md-button Angular Material
我有一个包含 MdDialog
组件的页面(比如父页面)。在对话框 window 中,我设置了一个 md-button
。
这个 md-button
可以在点击时触发父页面按钮的功能吗?
是的,您可以使用主题来触发事件。使用共享服务来发布和订阅主题。
像这样定义一个shared.service
:
import {Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable()
export class SharedService{
public triggerParentMethod: Subject<boolean> = new Subject<boolean>();
}
在你的 ParentComponent
中,订阅构造函数中的 triggerParentMethod
:
constructor(private sharedService:SharedService,public dialog: MdDialog){
this.sharedService.triggerParentMethod.subscribe( value =>{
if(value == true){
// Call some method here or some piece of code
console.log('called from dialog');
}
});
}
从您的对话中发出 triggerParentMethod
:
this.sharedService.triggerParentMethod.next(true);
完成working demo。
我有一个包含 MdDialog
组件的页面(比如父页面)。在对话框 window 中,我设置了一个 md-button
。
这个 md-button
可以在点击时触发父页面按钮的功能吗?
是的,您可以使用主题来触发事件。使用共享服务来发布和订阅主题。
像这样定义一个shared.service
:
import {Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable()
export class SharedService{
public triggerParentMethod: Subject<boolean> = new Subject<boolean>();
}
在你的 ParentComponent
中,订阅构造函数中的 triggerParentMethod
:
constructor(private sharedService:SharedService,public dialog: MdDialog){
this.sharedService.triggerParentMethod.subscribe( value =>{
if(value == true){
// Call some method here or some piece of code
console.log('called from dialog');
}
});
}
从您的对话中发出 triggerParentMethod
:
this.sharedService.triggerParentMethod.next(true);
完成working demo。