如何从角度7中的另一个组件关闭模态?

问题描述

是否有一种方法可以从其他组件关闭角材料模态?我已经尝试过使用服务,但它似乎不起作用。下面是我的代码

modal.service.ts

private modalClose = new Subject<any>();

setModalClose(){
    this.modalClose.next();
} 

getModalClose(){
    return this.modalClose.asObservable();
}

发送file.ts

onClick(){
    this.modalService.setModalClose();
}

接收file.ts

subscribe: Subscription;

constructor(
    private _modalService: ModalService
    private dialogRef: MatDialogRef<SaveDashboardModalComponent>){

    this.subscribe = this._modalService.getModalClose()
    .subscribe(()=>{
        this.dialogRef.close();
    })
    
}

解决方法

尝试这样的事情。

constructor(private dialog: MatDialog)

setModalClose(){
    this.dialog.close();
} 

// Here will be good if you open the dialog.

openModal() {
this.dialog.open(name of the dialog Component);

然后仅在另一个组件中调用。

this._modalService.setModalClose();