26

I have a following dialog component (which I open using dialog.open(MyDialogComponent) in another component):

export class MyDialogComponent implements OnInit {
  constructor(public matDialogRef: MatDialogRef<MyDialogComponent>) {}

  ngOnInit() {}
}

I know I can return any data to the calling component by executing matDialogRef.close(dataToReturn) bound to a button in dialog component. But how can I return the data if a user clicked elsewhere but the popup to close it?

4 Answers 4

40

If anybody is interested I found a solution (not sure if it is the best one). Just disabling the default close operation so the popup does not close on backround click, while closing it with data param on background click.

matDialogRef.disableClose = true;//disable default close operation
matDialogRef.backdropClick().subscribe(result => {
  matDialogRef.close(dataToReturn);
});

This way the calling component receives the data whether the dialog was closed by button or clicked elsewhere.

4
  • 3
    You should also subscribe to keyboard events to handle the esc key. I browsed through the source code and don't really see a better way to do this. Luckily, the backdrop click and esc keydown event are the only two things that need to be reimplemented to achieve the same functionality. Nov 18, 2017 at 18:50
  • 1
    Where do you set the dataToReturn object ?
    – Stephane
    Aug 22, 2018 at 13:49
  • Just above the matDialogRef.close(dataToReturn); line. Remember that the subscribing takes place at the time of creating the dialog (and usually you are returning a value which is not known at that time), so setting the return value should be inside following lambda function result => { let dataToReturn = ...; matDialogRef.close(dataToReturn); }) Aug 24, 2018 at 11:39
  • Unfortunately the return type must be manually specified when calling MatDialog.open<T, D, R>(). Upvote this feature request to infer return-type R based on component passed into MatDialog.open<T, D, R>()
    – JWess
    Apr 4, 2022 at 17:50
18

Call the close(dataToReturn) in the beforeClosed() to set the dialog result:

constructor(public matDialogRef: MatDialogRef<MyDialogComponent>) {
    matDialogRef.beforeClosed().subscribe(() => matDialogRef.close(this.dataToReturn));
}

This will work if the dialog is closed by clicking on the backdrop, or pressing the ESC.

5
  • 1
    This is the right solution in Angular Material 7+ (maybe lower versions too, not sure). Also you can get if the user clicked yes, no or just escaped (mat-dialog-close) by the first argument passed into the subscribe method: matDialogRef.beforeClose().subscribe((value) => matDialogRef.close(this.dataToReturn)); }
    – Asheq Reza
    Nov 1, 2019 at 19:13
  • 4
    this should be accepted answer. btw: typo, it should be beforeClosed
    – F.H.
    Oct 21, 2020 at 14:42
  • 1
    This is the right solution (Y) Sep 23, 2021 at 6:37
  • just FYI, beforeClosed does complete, so there is no need to unsubscribe
    – Ben White
    Apr 1, 2022 at 13:10
  • 1
    From Angular Material v15, this results in a 'maximum call stack size exceeded' error
    – fikkatra
    Jan 9 at 14:23
6
    //In dialog component.
    dataYouWantToReturn = true; //set data.
            
    this.dialogRef.close(dataYouWantToReturn); 
               
    //In component calling the dialog component.
    dialogRef.afterClosed().subscribe(result => {
        if (result) {
            console.log("Result is TRUE!");
        }
    });

You can also pass json objects such as below:

    //In dialog component.
    dataYouWantToReturn = {success: true, message: 'Result is TRUE!'}; //set data.
            
    this.dialogRef.close(dataYouWantToReturn); 
               
    //In component calling the dialog component.
    dialogRef.afterClosed().subscribe(result => {
        if (result.success) {
            console.log(result.message); //"Result is TRUE!"
        }
    });

   
1

Here's a solution that will return default value if no other value has been provided. Works with both backdrop and Esc clicks:

constructor(public dialogRef: MatDialogRef<DialogComponent>) {
    // return 'Cancel' when backdrop or 'Esc' clicked
    dialogRef.beforeClosed().subscribe(
       result => dialogRef.close(result ?? DialogResult.Cancel));
 }
    
  onYesClicked() {
    this.closeDialog(DialogResult.Yes);
  }

  onNoClicked() {
    this.closeDialog(DialogResult.No);
  }

  private closeDialog(result: DialogResult) {
    this.dialogRef.close(result);
  }

 export enum DialogResult {
   Cancel = -1,
   No = 0,
   Yes = 1,
 }
1
  • From Angular Material v15, this results in a 'maximum call stack size exceeded' error
    – fikkatra
    Jan 9 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.