首页 > 解决方案 > 如何在角度 7 中关闭另一个组件的模态?

问题描述

有没有办法从另一个组件关闭角材料模态?我已经尝试过使用服务,但它似乎不起作用。下面是我的代码。

modal.service.ts

private modalClose = new Subject<any>();

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

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

发送文件.ts

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

接收文件.ts

subscribe: Subscription;

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

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

标签: angularangular-materialangular-services

解决方案


尝试这样的事情。

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();

推荐阅读