0

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

<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>
4

1 回答 1

3

您必须使用 Angular 的 @ViewChild 装饰器来定位 mdbModal 指令,然后使用 ModalDirective 类中的 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();
  }

如果有不清楚的地方,欢迎再次提问!

于 2019-03-07T12:33:57.750 回答