简体   繁体   English

禁用在角度材质对话框区域外单击以关闭对话框(使用 Angular 版本 4.0+)

[英]Disable click outside of angular material dialog area to close the dialog (With Angular Version 4.0+)

I am currently working on password reset page of an Angular 4 project.我目前正在处理 Angular 4 项目的密码重置页面。 We are using Angular Material to create the dialog, however, when the client clicks out of the dialog, it will close automatically.我们使用 Angular Material 创建对话框,但是,当客户端单击对话框外时,它会自动关闭。 Is there a way to avoid the dialog close until our code side call "close" function?有没有办法避免对话框关闭,直到我们的代码端调用“关闭”函数? Or how should I create an unclosable modal?或者我应该如何创建一个不可关闭的模态?

There are two ways to do it.有两种方法可以做到。

  1. In the method that opens the dialog, pass in the following configuration option disableClose as the second parameter in MatDialog#open() and set it to true :在打开对话框的方法中,传入以下配置选项disableClose作为MatDialog#open()的第二个参数并将其设置为true

     export class AppComponent { constructor(private dialog: MatDialog){} openDialog() { this.dialog.open(DialogComponent, { disableClose: true }); } }
  2. Alternatively, do it in the dialog component itself.或者,在对话框组件本身中执行此操作。

     export class DialogComponent { constructor(private dialogRef: MatDialogRef<DialogComponent>){ dialogRef.disableClose = true; } }

Here's what you're looking for:这就是你要找的:

material.angular.io 中的 <code>disableClose</code> 属性

And here's a Stackblitz demo这是一个Stackblitz 演示


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允许esc关闭对话框但不允许单击背景关闭对话框

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.正如@MarcBrazeau 在我的回答下面的评论中所说的那样,您可以允许esc键关闭模态,但仍然不允许在模态外单击。 Use this code on your dialog component:在对话框组件上使用此代码:

import { Component, OnInit, HostListener } from '@angular/core';
import { MatDialogRef } from '@angular/material';
@Component({
  selector: 'app-third-dialog',
  templateUrl: './third-dialog.component.html'
})
export class ThirdDialogComponent {
  constructor(private dialogRef: MatDialogRef<ThirdDialogComponent>) {      
}
  @HostListener('window:keyup.esc') onKeyUp() {
    this.dialogRef.close();
  }

}

Prevent esc from closing the dialog but allow clicking on the backdrop to close防止esc关闭对话框但允许单击背景关闭

PS This is an answer which originated from this answer , where the demo was based on this answer. PS 这是源自此答案的答案,其中演示基于此答案。

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.为了防止esc键关闭对话框但允许点击背景关闭,我调整了 Marc 的答案,并使用MatDialogRef#backdropClick来监听MatDialogRef#backdropClick的点击事件。

Initially, the dialog will have the configuration option disableClose set as true .最初,对话框将配置选项disableClose设置为true This ensures that the esc keypress, as well as clicking on the backdrop will not cause the dialog to close.这可确保esc按键以及单击背景不会导致对话框关闭。

Afterwards, subscribe to the MatDialogRef#backdropClick method (which emits when the backdrop gets clicked and returns as a MouseEvent ).之后,订阅MatDialogRef#backdropClick方法(当背景被点击时发出并作为MouseEvent返回)。

Anyways, enough technical talk.无论如何,足够的技术谈话。 Here's the code:这是代码:

openDialog() {
  let dialogRef = this.dialog.open(DialogComponent, { disableClose: true });
  /*
     Subscribe to events emitted when the backdrop is clicked
     NOTE: Since we won't actually be using the `MouseEvent` event, we'll just use an underscore here
     See https://stackoverflow.com/a/41086381 for more info
  */
  dialogRef.backdropClick().subscribe(() => {
    // Close the dialog
    dialogRef.close();
  })

  // ...
}

Alternatively, this can be done in the dialog component:或者,这可以在对话框组件中完成:

export class DialogComponent {
  constructor(private dialogRef: MatDialogRef<DialogComponent>) {
    dialogRef.disableClose = true;
    /*
      Subscribe to events emitted when the backdrop is clicked
      NOTE: Since we won't actually be using the `MouseEvent` event, we'll just use an underscore here
      See https://stackoverflow.com/a/41086381 for more info
    */
    dialogRef.backdropClick().subscribe(() => {
      // Close the dialog
      dialogRef.close();
    })
  }
}

How about playing with these two properties?玩弄这两个属性怎么样?

disableClose: boolean - Whether the user can use escape or clicking on the backdrop to close the modal. disableClose: boolean - 用户是否可以使用转义或点击背景来关闭模式。

hasBackdrop: boolean - Whether the dialog has a backdrop. hasBackdrop: boolean - 对话框是否有背景。

https://material.angular.io/components/dialog/api https://material.angular.io/components/dialog/api

Add添加

[config]="{backdrop: 'static'}"

to the modal code.到模态代码。

This works for me 这对我有用

  openDialog() {
    this.dialog.open(YourComponent, { disableClose: true });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM