ng-bootstrap - 模态组件上的容器属性

问题描述 投票:1回答:1

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

请问你能帮帮我吗?

angular ng-bootstrap ngx-bootstrap ng2-bootstrap
1个回答
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)}`;
});
© www.soinside.com 2019 - 2024. All rights reserved.