Monday, 17 January 2022

Angular bootstrap ngbModal - prevent close modal when clicking outside of modal and pressing ESC

https://stackoverflow.com/questions/22207377/disable-click-outside-of-bootstrap-modal-area-to-close-modal 

https://stackoverflow.com/questions/41438160/stop-closing-the-modal-by-clicking-backdrop-or-outside-the-modal

https://stackoverflow.com/questions/41504481/restrict-ng-bootstrap-modal-from-closing-if-clicked-outside-or-esc-button

modalOption: NgbModalOptions = {}; // not null!

// ...

openAddModal() {
    this.modalOption.backdrop = 'static'; // prevent closing modal when click outside
    this.modalOption.keyboard = false; // prevent closing modal when pressing ESC
    const modalRef = this.modalService.open(PremiumProtectionComponent,this.modalOption);
}

On Options chapter, in the page you linked, you can see the backdrop option. Passing this option with value 'static' will prevent closing the modal.

As @PedroVagner pointed on comments, you also can pass {keyboard: false} to prevent closing the modal by pressing Esc.

No comments:

Post a Comment