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
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.
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.
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.
You can prevent closing of modal dialog by setting the beforeClose event argument cancel value to true.
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); }
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