Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material Dialog -> afterClosed -> which button was pressed

I use Angular Material in my application and also Angular Material dialogs. After closing a dialog either action A or action B should be executed depending on the button clicked:

dialogRef.afterClosed().subscribe(() => {
    // if close button was clicked do action A
    // if other button was clicked do action B
 })

Is there a possibility to detect which button was clicked in afterClosed method?

like image 671
quma Avatar asked Apr 09 '18 13:04

quma


People also ask

How to perform action after closing a dialog in Angular Material?

I use Angular Material in my application and also Angular Material dialogs. After closing a dialog either action A or action B should be executed depending on the button clicked: dialogRef.afterClosed ().subscribe ( () => { // if close button was clicked do action A // if other button was clicked do action B })

How do I create a dialog in Angular Material?

With Angular Material your dialogs are separate components, so the first step will be to create a component for your dialog. With the Angular CLI, you can do something like this: Then you’ll want to import the dialog component in your app module and in the component that will call the dialog.

What is the use of buttons in Angular Material?

Angular Material uses native <button> and <a> elements to ensure an accessible experience by default. The <button> element should be used for any interaction that performs an action on the current page. The <a> element should be used for any interaction that navigates to another view.

How do I report an issue with angular material?

You can help us out by using the "report an issue" button at the bottom of the tutorial. We went over many of the available components with Angular Material, but dialogs were left out because they are a little bit more involved to setup than most of the other Angular Material components.


2 Answers

You can close the dialog with custom data. Like this:

in your dialog component:

@Component({/* ... */})
export class YourDialog {
  constructor(public dialogRef: MatDialogRef<YourDialog>) { }


  closeA() {
    this.closeDialog('A')
  }

  closeB() {
    this.closeDialog('B');
  }

  closeDialog(button: 'A' | 'B') {
    this.dialogRef.close(button);
  }
}

handle close like this:

dialogRef.afterClosed().subscribe(result => {
  if (result === 'A') {
    // handle A button close
  }

  if (result === 'B')
    // handle B button close
  }
});

And since afterClosed() is an observable, you can filter this stream to create a more declarative solution:

const closedA$ = dialogRef.afterClosed().pipe(filter(result => result === 'A'));
const closedB$ = dialogRef.afterClosed().pipe(filter(result => result === 'B'));

closedA$.subscribe( // handle A);
closedB$.subscribe( // handle B);
like image 129
Tomasz Kula Avatar answered Sep 24 '22 19:09

Tomasz Kula


The way that I currently using is to pass an string from dialog while closing like :

this.dialogRef.close('A or B or whatever');

and I use them outside like this :

  dialogRef.afterClosed().subscribe((result: any) => {
    if (resuld === 'A'){
      // if close button was clicked do action A
    } else if (resuld === 'B') {
      // if other button was clicked do action B
    }
 })
like image 23
Arash Avatar answered Sep 24 '22 19:09

Arash