Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Prevent MatDialog From Closing When Clicked Outside

How to prevent MatDialog from closing when clicked outside but in all the dialogs in my Angular application ? I also realized that the escape key is not closing the dialog after setting disableClose to true so i added a hostlistener to force the close but it's not really the best solution... For a specific component I can do this.

export class MyAppComponent {  
  constructor(private dialog: MatDialog){}  
  open() {  
    this.dialog.open(ConfirmComponent, { disableClose: true });  
  }  
 
  @HostListener('window:keyup.esc') onKeyUp() {
    this.dialogRef.close();
  }

}

but how to do it globally for all the dialogs in my application instead of doing it in each dialog component and also apply the hostlistener?

like image 837
Kivo Avatar asked Oct 25 '25 12:10

Kivo


1 Answers

You can define global setting in provider

import { MAT_DIALOG_DEFAULT_OPTIONS } from '@angular/material/dialog';



@NgModule({
  providers: [
    { provide: MAT_DIALOG_DEFAULT_OPTIONS, useValue: { disableClose: true }}
  ]
})
like image 144
Gajendra Avatar answered Oct 28 '25 05:10

Gajendra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!