首页 > 解决方案 > 非法调用实例化 BS 模态

问题描述

我试图通过组件以角度调用引导模式,而不是使用它的 HTML 属性。我收到一个错误(即非法调用)。

零件:

@ViewChild('myModal') div:any;

ngAfterViewInit() {let bs = new bootstrap.Modal(this.div);}

HTML

<div class="modal-content" #myModal>...</div>

标签: javascriptangulartypescripttwitter-bootstrap

解决方案


您需要使用该服务而不是创建它的一个实例。对代码进行以下更改:

import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap';
...
...
@ViewChild('myModal') div:any;

constructor(private modalService: NgbModal )

ngAfterViewInit() {
     // make sure you get the instance of the child.
     this.modalService.open(this.div);  
}

推荐阅读