【问题标题】:catching dismiss event ngx-bootstrap modal [closed]捕捉解雇事件ngx-bootstrap模式[关闭]
【发布时间】:2019-02-26 08:40:50
【问题描述】:

我正在使用 Angular 6 和 ngx-bootstrap 3.0.1

我显示一个模式,并且我希望能够在用户在更新表单后尝试关闭模式时显示放弃/取消确认。

当用户使用我的自定义关闭按钮时我没有问题,但是当他在模式外使用背景点击时我不知道如何调用我的关闭函数。

如何干净利落地处理背景点击以显示我的确认消息?

【问题讨论】:

标签: angular twitter-bootstrap bootstrap-modal ngx-bootstrap


【解决方案1】:

使用选项打开您的模式

this.bsModalRef = this.modalService.show(ExampleModal, {
  ignoreBackdropClick: true,
  keyboard: false
});

在 ExampleModal 中放置此代码

ngOnInit(): void {
     document.documentElement.addEventListener('click',  this.hideIfIsBackdropClick.bind(this)); 
}

hideIfIsBackdropClick(event: any): void {
     if (event.target.classList.contains('modal')) {
        this.hide();
     }
}

hide(): void {
   if (anyBooleanHere) {
      this.bsModalRef.hide();
   }
}

【讨论】:

    【解决方案2】:

    在 ngx-bootstrap 文档页面,您可以轻松找到您的解决方案:https://valor-software.com/ngx-bootstrap/#/modals#directive-events

    要查看触发了哪些事件,

    在您的组件方面,

    import { Component, ViewChild } from '@angular/core';
    import { ModalDirective } from 'ngx-bootstrap/modal';
    
    @Component({
      selector: 'demo-modal-events',
      templateUrl: './events.html',
      styles: [`
        .card {
          margin-bottom: 0.75rem;
          padding: 8px;
        }
      `]
    })
    export class DemoModalEventsComponent {
      @ViewChild(ModalDirective) modal: ModalDirective;
      messages: string[];
    
      showModal() {
        this.messages = [];
        this.modal.show();
      }
      handler(type: string, $event: ModalDirective) {
        this.messages.push(
          `event ${type} is fired${$event.dismissReason
            ? ', dismissed by ' + $event.dismissReason
            : ''}`
        );
      }
    }
    

    在您的模板方面,

    <button type="button" class="btn btn-primary" (click)="showModal()">Open a modal</button>
    <br><br>
    <pre class="card card-block card-header" *ngFor="let message of messages">{{message}}</pre>
    
    <div class="modal fade" bsModal #modal="bs-modal"
         tabindex="-1" role="dialog" aria-labelledby="dialog-events-name"
         (onShow)="handler('onShow', $event)"
         (onShown)="handler('onShown', $event)"
         (onHide)="handler('onHide', $event)"
         (onHidden)="handler('onHidden', $event)">
      <div class="modal-dialog modal-sm">
        <div class="modal-content">
          <div class="modal-header">
            <h4 id="dialog-events-name" class="modal-title pull-left">Modal</h4>
            <button type="button" class="close pull-right" aria-label="Close" (click)="modal.hide()">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            Just another modal <br>
            Click <b>&times;</b>, press <code>Esc</code> or click on backdrop to close modal.
          </div>
        </div>
      </div>
    </div>
    

    更新:

    据我了解,您可以像这样检查 $event:

    if($event.dismissReason == 'backdrop-click')
    this.myFunc();
    

    【讨论】:

    • onHide 事件在隐藏操作开始时触发,我想要的是当用户在窗口外单击时执行我自己的关闭代码,因为我希望能够取消隐藏。
    • @ArthurSaint-Genis 我已经更新了我的答案,请检查一下。
    • 您可以在禁用背景点击后执行此操作以不关闭窗口,请参见此处(背景选项):valor-software.com/ngx-bootstrap/#/…
    • 从组件调用modal怎么样?
    【解决方案3】:

    您可以使用钩子ngOnDestroy() 来检测用户何时关闭模态

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 2019-04-05
      • 2014-09-07
      • 2011-10-19
      • 1970-01-01
      • 2018-03-05
      相关资源
      最近更新 更多