I am currently worked on password reset page of an Angular 4 project. We are using Angular Material to create the dialog, however, when the client clicks out of the dialog, it will close automatically. Is there a way to avoid the dialog close until our code side call "close" function? Or how should I create an uncloseable modal?
相关问题
- Angular RxJS mergeMap types
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- How to update placeholder text in ng2-smart-table?
- How to instantiate Http service in main.ts manuall
- Angular: ngc or tsc?
相关文章
- angular脚手架在ie9+下兼容问题
- angular 前端项目 build 报错 "Cannot find module 'le
- Angular Material Stepper causes mat-formfield to v
- After upgrade to Angular 9 cannot find variable in
- is there any difference between import { Observabl
- Suppress “Circular dependency detected” suppress w
- How can you get current positional information abo
- Angular material table not showing data
This works for me
RTFM
According to the documentation here you can use the
disableClose
input to prevent the user from closing the dialog.There are two ways to do it.
In the method that opens the dialog, pass in the following configuration option
disableClose
as the second parameter inMatDialog#open()
and set it totrue
:Alternatively, do it in the dialog component itself.
Here's what you're looking for:
And here's a Stackblitz demo
Other use cases
Here's some other use cases and code snippets of how to implement them.
Allow esc to close the dialog but disallow clicking on the backdrop to close the dialog
As what @MarcBrazeau said in the comment below my answer, you can allow the esc key to close the modal but still disallow clicking outside the modal. Use this code on your dialog component:
Prevent esc from closing the dialog but allow clicking on the backdrop to close
To prevent the esc key from closing the dialog but allow clicking on the backdrop to close, I've adapted Marc's answer, as well as using
MatDialogRef#backdropClick
to listen for click events to the backdrop.Initially, the dialog will have the configuration option
disableClose
set astrue
. This ensures that theesc
keypress, as well as clicking on the backdrop will not cause the dialog to close.Afterwards, subscribe to the
MatDialogRef#backdropClick
method (which emits when the backdrop gets clicked and returns as aMouseEvent
).Anyways, enough technical talk. Here's the code:
Alternatively, this can be done in the dialog component: