Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict ng-bootstrap modal from closing if clicked outside or ESC button

Tags:

I am using ng-bootstrap to open a modal for my angular2 project. The modal is getting dissolved if I click outside and also ESC button. I want to restrict these two behavior. Below is my code to open the modal

import { NgbModal, NgbModalOptions } from '@ng-bootstrap/ng-bootstrap'; ..... modalOption: NgbModalOptions = null; constructor( private modalService: NgbModal, .... } .... //the method I am using to open a modal openAddModal() {     this.modalOption.backdrop = 'static';     this.modalOption.keyboard = false;     const modalRef =  this.modalService.open(PremiumProtectionComponent,this.modalOption);     ..... } 

PremiumProtectionComponent is the ts file for the modal.

Below is the HTML where I have called the method

<a (click)="openAddModal()">Click to open modal</a>

Can anybody suggest me the correct way to achieve this behavior. Thanks in advance

like image 290
AriSan Avatar asked Jan 06 '17 11:01

AriSan


People also ask

How do I make Bootstrap modal not close on click outside?

When the Button is clicked, the HTML DIV is referenced using jQuery and its modal function is called along with properties data-backdrop: "static" and data-keyboard: false which disables the closing of the Bootstrap Modal Popup when clicked outside.

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 I keep my Bootstrap modal from closing when I click the button?

To prevent closing bootstrap modal when click outside of modal window we need add properties data-backdrop="static" and data-keyboard="false" to button which we are using to show modal window like as shown following.

How do you stop a modal closing?

You can prevent closing of modal dialog by setting the beforeClose event argument cancel value to true.


1 Answers

You need to set backdrop and keyboard properties on modalOptions object, not null:

modalOption: NgbModalOptions = {}; // not null!  // ...  openAddModal() {     this.modalOption.backdrop = 'static';     this.modalOption.keyboard = false;     const modalRef = this.modalService.open(PremiumProtectionComponent,this.modalOption); } 
like image 67
dfsq Avatar answered Sep 29 '22 16:09

dfsq