Angular 7 在打字稿中隐藏 bootstrap 模态

Angular 7 Hide bootstrap modal in typscript

我想在我的打字稿组件中的一个功能中调用 (click)="basicModal.hide()" MDBootstrap Modal 的方法。我不知道如何从组件访问此方法。

<button type="button" mdbBtn color="primary" class="relative waves-light" (click)="basicModal.show()" mdbWavesEffect>Launch demo modal</button>

<div mdbModal #basicModal="mdbModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myBasicModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close pull-right" aria-label="Close" (click)="basicModal.hide()">
          <span aria-hidden="true">×</span>
        </button>
        <h4 class="modal-title w-100" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" mdbBtn color="secondary" class="waves-light" aria-label="Close" (click)="basicModal.hide()" mdbWavesEffect>Close</button>
        <button type="button" mdbBtn color="primary" class="relative waves-light" mdbWavesEffect>Save changes</button>
      </div>
    </div>
  </div>
</div>

您必须使用 Angular 的 @ViewChild 装饰器来定位 mdbModal 指令,然后使用 ModalDirective class 中的 hide() 方法。

请看下面的代码,里面解释了一切:

.html:

<button type="button" mdbBtn color="primary" class="relative waves-light" (click)="basicModal.show()" mdbWavesEffect>Launch demo modal</button>

<div #modal mdbModal #basicModal="mdbModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myBasicModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close pull-right" aria-label="Close" (click)="basicModal.hide()">
          <span aria-hidden="true">×</span>
        </button>
        <h4 class="modal-title w-100" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        <div class="md-form">
          <input type="text" class="form-control" id="input1" mdbInput>
          <label for="input1">Modal input</label>
        </div>
      </div>
      <div class="modal-footer">
        <button mdbBtn color="primary" mdbWavesEffect (click)="hideModal()">Hide modal</button>
      </div>
    </div>
  </div>
</div>

.ts:

 @ViewChild(ModalDirective) modal: ModalDirective;

  hideModal() {
    this.modal.hide();
  }

如有不明之处,欢迎再问!