在ng-bootstrap站点上,Modal component
-> Api
上有一条信息,表明模态具有属性conatainer
,描述为“ An element to which to attach newly opened modal windows
”。这是否意味着我可以将打开的模式直接附加到HTML网站上指定的div id元素上?
如果是这样,如何在Angular项目上实现? 在我的项目中,我在modal-basic.html中添加了以下代码:
<div id="test" class="col-6">Some text</div>
还添加了属性容器:
this.modalService.open(content, {container: 'test',ariaLabelledBy: 'modal-basic-title'}).result.then((result) => {
this.closeResult = `Closed with: ${result}`;
}, (reason) => {
this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
});
但是我收到一个错误:
NgbdModalBasic.html:28 ERROR Error: The specified modal container "test" was not found in the DOM.
at NgbModalStack.push../node_modules/@ng-bootstrap/ng-bootstrap/fesm5/ng-bootstrap.js.NgbModalStack.open (ng-bootstrap.js:6417)
at NgbModal.push../node_modules/@ng-bootstrap/ng-bootstrap/fesm5/ng-bootstrap.js.NgbModal.open (ng-bootstrap.js:6681)
at NgbdModalBasic.push../src/app/modal-basic.ts.NgbdModalBasic.open (modal-basic.ts:16)
at Object.eval [as handleEvent] (NgbdModalBasic.html:28)
at handleEvent (core.js:10258)
at callWithDebugContext (core.js:11351)
at Object.debugHandleEvent [as handleEvent] (core.js:11054)
at dispatchEvent (core.js:7717)
at core.js:8161
at HTMLButtonElement.<anonymous> (platform-browser.js:995)
能帮我吗?
答案 0 :(得分:2)
指定容器元素时,您需要传入元素或选择器或两者的组合。在您的情况下,您想访问ID为test的元素。容器属性应设置为“ #test”。
this.modalService.open(content, {container: '#test',ariaLabelledBy: 'modal-basic-title'}).result.then((result) => {
this.closeResult = `Closed with: ${result}`;
}, (reason) => {
this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
});