Angular 2+:通过打字稿打开Bootstrap模式

时间:2019-02-11 16:25:29

标签: javascript angular twitter-bootstrap typescript ng-bootstrap

通常,当打开模式时,通过HTML调用该函数:

<button class="btn btn-lg btn-outline-primary" (click)="open(content)">Launch demo modal</button>

这将打开模式:

  <ng-template #content let-modal>
      <div class="modal-header">
        <h4 class="modal-title" id="modal-basic-title">Profile update</h4>
        <button type="button" class="close" aria-label="Close" (click)="modal.dismiss('Cross click')">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form>
          <div class="form-group">
            <label for="dateOfBirth">Date of birth</label>
            <div class="input-group">
              <input id="dateOfBirth" class="form-control" placeholder="yyyy-mm-dd" name="dp" ngbDatepicker #dp="ngbDatepicker">
              <div class="input-group-append">
                <button class="btn btn-outline-secondary calendar" (click)="dp.toggle()" type="button"></button>
              </div>
            </div>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-outline-dark" (click)="modal.close('Save click')">Save</button>
      </div>
    </ng-template>

但是,由于某些情况(我没有使用Google Charts和可以使用(click) = "open(content"的HTML),我需要能够通过javascript / typescript打开模式。 / p>

我试图这样做:

  open(content) {

    this.modalService.open(content, {ariaLabelledBy: 'modal-basic-title'}).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

  select(event: ChartSelectEvent) {

        this.open(content); //<--Attempt to Open Modal

        if ('deselect' === event.message) {    
        }
        else if ('select' === event.message) {

        }
      }

我的尝试没有打开模式。因此,我认为我需要做的是如何通过打字稿调用template reference variable (#content),但我不确定如何

1 个答案:

答案 0 :(得分:2)

您可以使用@ViewChild("content")获得对模板的引用:

@ViewChild("content") private contentRef: TemplateRef<Object>;

并使用该变量打开模式:

this.open(this.contentRef);

有关演示,请参见this stackblitz