I have used one angular2 ng-bootstrap modal in our code.
I want the modal will not close when I click outside the modal. Currently the modal is getting closed while clicking outside.
In angular1 I used to do this by [keyboard]="false" [backdrop]="'static'". But this is time it is not working in angular2.
Here is my plunker
My Open method is as follows:
open() { const modalRef = this.modalService.open(NgbdModalContent); modalRef.componentInstance.name = 'World'; }
Data-keyword="false" is to prevent closing modal while clicking Esc button, while data-backdrop="static" , allows you keep modal pop-up opened when clicking on Gray area.
Another way to dismiss the modal when clicking outside involved taking advantage of the bubbling nature of the javascript events. clicking on . modal will cause the click event to propagate like this . modal -> #modal-root -> body while clicking outside the modal will only go through #modal-root -> body .
Static backdropWhen backdrop is set to static, the modal will not close when clicking outside it. Click the button below to try it.
There are few ways to close modal in Bootstrap: click on a backdrop, close icon, or close button. You can also use JavaScript hide method. Click the button to launch the modal. Then click on the backdrop, close icon or close button to close the modal.
As per the answer of @anshuVersatile, I understand we need to use some modal options.
Then I create a object of NgbModalOptions and pass it as second parameter of my open method and it works.
Code is as follows :
let ngbModalOptions: NgbModalOptions = { backdrop : 'static', keyboard : false }; console.log(ngbModalOptions); const modalRef = this.modalService.open(NgbdModalContent, ngbModalOptions);
Here is the updated plunker.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With