Angular2 Material Dialog Box

I used the angular2 material of MdDialog to show the shape.

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

I can close the dialog using the button as shown below.

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

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

The component that appears inside the dialog box does not have a ref dialog, and I do not know of any other way to close the dialog box from the component myself.

Is there a way to close a dialog box inside a component inside a dialog box?

+7
angular angular-material2
source share
1 answer

If you want to close it from the dialog:

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

If you want to close it from the parent of the dialog box:

 constructor(private matDialog: MatDialog){} //anywhere let dialogRef = this.matDialog.open(MyDialogComponent); dialogRef.close(); 
+15
source share

All Articles