Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 material dialog self close

I used angular2 material MdDialog to show a form.

When user submits the form, a request is sent to the backend and if the request is successful, I need to close the dialog. If backend request failed, I need to keep the dialog open.

I can close the dialog by using a button like below.

<button md-raised-button md-dialog-close>Cancel</button> 

But, in this case, I only need to close the dialog only if the backend request is successful, so I need a way to programmatically close the dialog.

The component which is shown inside the dialog doesn't have the dialog ref, and I don't know any other way to self close the dialog from the component.

Is there any way I an close the dialog from within the component inside the dialog?

like image 534
Lahiru Chandima Avatar asked Jun 24 '17 10:06

Lahiru Chandima


People also ask

How do I get data from MatDialog?

You can pass any kind of data to the component by adding data to the MatDialog's open() options. }, }); You can retrieve the data by injecting MAT_DIALOG_DATA in the opened component.

How do you use mat dialog?

Approach: First we need to import 'MatDialog' from '@angular/material/dialog' and we need to create an instance for it in the constructor. Using this instance we can open the dialog box component. Now create a separate component for the dialog and write code as per the requirements.


1 Answers

If you wanna close it from the dialog:

constructor(private dialogRef: MatDialogRef<MyDialogComponent>){ }  closeDialog(){   this.dialogRef.close(); } 

If you wanna close it from the parent of the dialog:

constructor(private matDialog: MatDialog){}  //anywhere let dialogRef = this.matDialog.open(MyDialogComponent); dialogRef.close(); 
like image 57
Ploppy Avatar answered Sep 23 '22 22:09

Ploppy