简体   繁体   English

使用转义键关闭 Angular 模态的问题

[英]Problems with escape key to close Angular modal

I have a component Escrituracao to keep tab of a client's bills.我有一个组件 Escrituracao 来记录客户的账单。 It has this mat-table to show all the proper data.它有这个垫表来显示所有正确的数据。 To create a new bill a modal, CadastrarLancamentoComponent, is opened:要创建一个新账单,打开一个模式 CadastrarLancamentoComponent:

openModalLancamento(data) {
    const modalOptions: NgbModalOptions = {};
    modalOptions.backdrop = 'static';
    const modalRef = this.modalService.open(CadastrarLancamentoComponent, modalOptions);
    modalRef.result.then((result) => {
      if (result) {
        this.getLancamentosByPeriod();
      }
    }, (result) => {
      if (result) {
        //escape result enters here
        this.getLancamentosByPeriod();
      }
    });
  }

When a new bill is added the modal is kept open to add more bills (that's intended).添加新账单时,模式保持打开状态以添加更多账单(这是预期的)。 However, when closed with the close or cancel button, if a bill was added, the modal returns a specific result value enabling the page to refresh (thus calling this.getLancamentosByPeriod();).但是,当使用关闭或取消按钮关闭时,如果添加了账单,模式会返回一个特定的结果值,使页面能够刷新(因此调用 this.getLancamentosByPeriod();)。 When using cancel or close button with no bill added, it only closes the modal without reloading.当使用取消或关闭按钮而不添加账单时,它只会关闭模式而不重新加载。

My main struggle is when using the Escape key.我的主要困难是使用 Escape 键时。 When used, both when added or not a bill, it only closes the modal.使用时,无论是添加还是不添加账单,它都只会关闭模式。 The result is the same for both situations.两种情况的结果是相同的。 I've tried using some Output data transfer to the Financeiro component to say a bill was added;我尝试使用一些输出数据传输到 Financeiro 组件来说明添加了账单; and tried accessing the modal's data to fetch a boolean that would give me that information.并尝试访问模态数据以获取一个布尔值,该布尔值将为我提供该信息。 All with no solution.都无解。

Is there a way to force a value on this modal result when closed with the escape key?当使用转义键关闭时,有没有办法在这个模态结果上强制一个值? Or to send this information proper (like used on the other close buttons) to the main page?或者将这些信息正确地发送到主页(就像在其他关闭按钮上使用的那样)? What I need is the page to behave the same as the other closing buttons, to reload on close when a bill was added.我需要的是页面与其他关闭按钮的行为相同,以便在添加账单时重新加载关闭。

I'm using Angular/Typescript for this project.我正在为这个项目使用 Angular/Typescript。

Are you using material for your dialog, custom, or something else?你是在为你的对话、自定义或其他东西使用材料吗?

If it's material, then I believe there's and option to disable escape being used to close it.如果它很重要,那么我相信可以选择禁用逃逸来关闭它。

Yeah, looked it up, disableClose is the option.是的,查了一下, disableClose是选项。 That removes escape and clicking outside of the dialog as ways to close it.这消除了转义和单击对话框外部作为关闭它的方法。

Once disabled, put your own escape key listener in there - something like this I guess - and when escape is detected, directly call your desired, custom close method.一旦禁用,将您自己的转义键侦听器放在那里 - 我猜是这样的 - 当检测到转义时,直接调用您想要的自定义关闭方法。

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

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