How To Prevent MatDialog From Closing When Clicked Outside

By default, you could close a material dialog box by clicking outside dialog box element or by pressing the esape key. However, in certain situations you may want to prevent this from happening.
 
Angular Material comes with a built-in property called disableClose the prevents this behavior.
  1. export class MyAppComponent {  
  2.   constructor(private dialog: MatDialog){}  
  3.   open() {  
  4.     this.dialog.open(ConfirmComponent, { disableClose: true });  
  5.   }  
  6. }  
As you can see in the above code snippet, we're passing an object as the second parameter to open method which contains disableClose property set to true.
  1. export class ConfirmComponent {  
  2.   constructor(private dialogRef: MatDialogRef<ConfirmComponent>){  
  3.     dialogRef.disableClose = true;  
  4.   }  
  5. }  
You could also set the disableClose property to true in the dialog component.