Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stop closing the modal by clicking backdrop or outside the modal

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';   } 
like image 279
Partha Sarathi Ghosh Avatar asked Jan 03 '17 07:01

Partha Sarathi Ghosh


People also ask

How do I stop closing the modal when I click outside?

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.

How do you close the modal by clicking outside of the modal box?

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 .

How can you prevent bootstrap modal from closing when clicking outside the content area?

Static backdropWhen backdrop is set to static, the modal will not close when clicking outside it. Click the button below to try it.

How do you close a modal by clicking on a button?

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.


1 Answers

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.

like image 127
Partha Sarathi Ghosh Avatar answered Sep 20 '22 18:09

Partha Sarathi Ghosh