Angular6 如何在angular中从子级关闭父级对话框?

Angular6 如何在angular中从子级关闭父级对话框?,angular6,angular-material-5,Angular6,Angular Material 5,我有一个父对话框,其中有一个子对话框。 在子对话框中有一个关闭按钮 单击此关闭按钮后,我想同时关闭父和子对话框。如何在angular6中执行此操作?您只需将父对话框的MatDialogRef传递给对话框数据中的子对话框组件,并在子组件代码中关闭该组件即可。 请查找下面的代码 这是父组件对话框的代码,它打开子对话框并将父MatDialogRef发送到数据中的子对话框组件: @组成部分({ 选择器:“确认对话框”, templateUrl:'confirmation dialog.html', }

我有一个父对话框,其中有一个子对话框。 在子对话框中有一个关闭按钮


单击此关闭按钮后,我想同时关闭父和子对话框。如何在angular6中执行此操作?

您只需将父对话框的MatDialogRef传递给对话框数据中的子对话框组件,并在子组件代码中关闭该组件即可。 请查找下面的代码

  • 这是父组件对话框的代码,它打开子对话框并将父MatDialogRef发送到数据中的子对话框组件:
  • 
    @组成部分({
    选择器:“确认对话框”,
    templateUrl:'confirmation dialog.html',
    })
    导出类确认对话框{
    childDilogRef=null;
    message:string=“你确定吗?”
    confirButtonText=“是”
    cancelButtonText=“取消”
    建造师(
    公共对话:MatDialog,
    @注入(MAT_DIALOG_DATA)私有数据:任意,
    private parentDilogRef:MatDialogRef){
    如果(数据){
    this.message=data.message | | this.message;
    if(data.buttonText){
    this.confirButtonText=data.buttonText.ok | | this.confirButtonText;
    this.cancelbuttonext=data.buttonext.cancel | | this.cancelbuttonext;
    }
    }
    }
    onConfirmClick():void{
    this.parentDilogRef.close(true);
    }
    //此方法用于打开子对话框
    OpenChild(){
    if(this.childDilogRef==null){
    this.childDilogRef=this.dialog.open(MyChildComponent{
    data:this.parentDilogRef,//父对话框作为数据发送到子对话框组件
    });
    this.childDilogRef.afterClosed().subscribe(结果=>{
    this.childDilogRef=null;
    });
    }
    }
    }
    
  • 这是将提供的ParentDialogRef初始化为本地dialogRef变量的子组件的代码。我们在点击子对话框上的按钮时关闭对话框ref
  • 
    @组成部分({
    选择器:“子对话框”,
    模板:`
    
    单击按钮关闭两个对话框
    

    关闭两个对话框 `, }) 导出类MyChildComponent{ 建造师( public childDialogRef:MatDialogRef, 公共parentDialogRef:MatDialogRef, @注入(MAT_DIALOG_数据)公共数据:MatDialogRef ) { 如果(数据){ this.parentDialogRef=数据 } } //关闭“关于”对话框 onNoClick():void{ this.childDialogRef.close(); } closeBoth():void{ this.childDialogRef.close(); this.parentDialogRef.close(); } }
    在我的案例中:

    家长:

    const dialogRef=this.dialog.open(AssignResourcePageComponent);
    dialogRef.componentInstance.modal\u principal\u parent.on('CLOSE\u parent\u modal',()=>{
    dialogRef.close();
    
    });如果它对您有效,请将其标记为答案,以便其他人可以遵循