Angular 2+: 通过打字稿打开 Bootstrap 模态
Angular 2+: Opening Bootstrap modal through typescript
通常情况下,当打开一个模态时,通过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">×</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 图表,我可以使用 (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) {
}
}
我的尝试没有打开模式。所以,我认为我需要做的是如何通过打字稿调用 template reference variable (#content)
但我不确定如何
您可以通过 @ViewChild("content")
:
获得对模板的引用
@ViewChild("content") private contentRef: TemplateRef<Object>;
并使用该变量打开模式:
this.open(this.contentRef);
有关演示,请参阅 this stackblitz。
通常情况下,当打开一个模态时,通过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">×</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 图表,我可以使用 (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) {
}
}
我的尝试没有打开模式。所以,我认为我需要做的是如何通过打字稿调用 template reference variable (#content)
但我不确定如何
您可以通过 @ViewChild("content")
:
@ViewChild("content") private contentRef: TemplateRef<Object>;
并使用该变量打开模式:
this.open(this.contentRef);
有关演示,请参阅 this stackblitz。