Ask Your Question
2

How can we close a nested MatDialog in Angular?

asked 2023-05-03 15:16:16 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-05-03 15:36:01 +0000

lakamha gravatar image

To close a nested MatDialog in Angular, you can use the MatDialogRef instance of the nested dialog and call the close() method on it.

Here is an example:

  1. In the parent component, open the nested dialog using the MatDialog service:
import { MatDialog } from '@angular/material/dialog';
import { NestedDialogComponent } from './nested-dialog/nested-dialog.component';

@Component({
  ...
})
export class ParentComponent {
  constructor(private dialog: MatDialog) {}

  openNestedDialog() {
    const dialogRef = this.dialog.open(NestedDialogComponent);
  }
}
  1. In the nested dialog component, inject the MatDialogRef instance and use it to close the dialog:
import { Component, OnInit, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';

@Component({
  ...
})
export class NestedDialogComponent implements OnInit {
  constructor(private dialogRef: MatDialogRef<NestedDialogComponent>) {}

  ngOnInit() {}

  closeNestedDialog() {
    this.dialogRef.close();
  }
}
  1. In the nested dialog template, call the closeNestedDialog() method on a button click or any other event:
<button mat-button (click)="closeNestedDialog()">Close nested dialog</button>

This will close the nested dialog and return control to the parent component.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-03 15:16:16 +0000

Seen: 9 times

Last updated: May 03 '23