javascript - ng-bootstrap 模式在 ngOnInit() 上打开
问题描述
我想从我的打字稿代码中打开模态,即当组件加载到 ngOnit() 时,模态弹出窗口应该打开,我不想在 html 文件上创建按钮。
HTML:
<button class="btn btn-outline-primary mb-2 mr-2" (click)="openSm(content)">Small modal</button>
<ng-template #content let-modal>
<div class="modal-header">
<h4 class="modal-title">Modal title</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">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light" (click)="modal.close('Close click')">Close</button>
</div>
</ng-template>
TS:
openSm(content) {
this.modalService.open(content, { size: 'sm' });
}
我想在 ngOnit() 函数上打开模式。
解决方案
只需使用 ViewChild 获取“内容”并在 ngOnInit 调用 this.open(this.content)
@ViewChild("content",{static:true}) content:ElementRef;
constructor(private modalService: NgbModal) {}
ngOnInit()
{
this.openSm(this.content)
}
你的分叉堆栈闪电战
注意:我使用{static:true}
并将代码放在 ngOnInit 中,因为我认为您的“内容”始终是“可见的”,否则您需要使用 ngAfterViewInit
推荐阅读
- php - 如何在 SQL/PHP 中更改 Day-D-Month-Y - 时间日期格式
- spring - Spring Oauth2 不适用于 JBOSS 6.10 企业版
- laravel - 数据表选择 - 不选择行或复选框,cdn 加载...一切正常,复选框显示但不可选择
- r - 如何更改 R 向量的索引?
- django - Django 表单中动作属性的表示法
- svelte - TypeError: each_blocks[i] 在 Svelte 中为空
- webpack - Webpack4 Dev Build 挂起
- java - 对有序数组进行线性搜索的java实现
- performance - 添加的代码显着减慢了处理时间
- javascript - How to get socket.io to only listen on one specific route?