gpt4 book ai didi

javascript - Angular 2+ : Opening Bootstrap modal through typescript

转载 作者:行者123 更新时间:2023-11-30 11:08:45 26 4
gpt4 key购买 nike

通常,当打开一个模式时,该函数是通过 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 打开模式。

我尝试这样做:

  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) {

}
}

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

最佳答案

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

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

并使用该变量打开模式:

this.open(this.contentRef);

参见 this stackblitz用于演示。

关于javascript - Angular 2+ : Opening Bootstrap modal through typescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54634885/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com