Search code examples
angularng-bootstrapngx-bootstrapng2-bootstrap

ng-bootstrap - Container properties on Modal Component


On ng-bootstrap site, Modal component -> Api there is an information that modal has properties conatainer with descriprion: "An element to which to attach newly opened modal windows". Does it mean that I can attach opened modal directly to specified div id element on my HTML site?

If so, how to acheive that on Angular project? In my project I added in my modal-basic.html this code:

<div id="test" class="col-6">Some text</div>

And added properties container also:

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)}`;
});

But I'm receiving an error:

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)

Could you help me please?


Solution

  • When specifying the container element you need to pass in either the element or a selector or a combination of both. In your case you want to reach elements with an ID of test. The container property should be set to "#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)}`;
    });