38

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';
  }
Partha Sarathi Ghosh
  • 10,936
  • 20
  • 59
  • 84

5 Answers5

107

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.

Partha Sarathi Ghosh
  • 10,936
  • 20
  • 59
  • 84
  • 3
    Note that there's a similar, but completely independent angular bootstrap library 'ngx-bootstrap'. The options you need to set are different for this - see https://valor-software.com/ngx-bootstrap/#/modals#service-disable-backdrop. You'll need `let config = { backdrop: true, ignoreBackdropClick: true };` then ... `this.modalService.show(SomeComponent, config);` – Chris Halcrow May 22 '18 at 06:00
  • loading... in plunker => Error {originalErr: Error, constructor: Object} – Thilina Sampath Jul 10 '18 at 04:37
15
$modal.open({
   // ... other options
   backdrop  : 'static',
   keyboard  : false
});

While you creating your modal you can specify its behavior:

anshuVersatile
  • 2,030
  • 1
  • 11
  • 18
12

Though it is late still it might be helpful for somebody else facing the issue:

 const config: ModalOptions = {
                backdrop: 'static',
                keyboard: false,
                animated: true,
                ignoreBackdropClick: true,
                initialState: {
                  data1: 'new-user',
                  username: 'test'
                }
              };
              this.bsModalRef = this.modalService.show(MyComponent, config);

initialState object is used to pass data to modal.

Anuj Shukla
  • 271
  • 3
  • 7
5

In case of ngx-bootstrap/modal in angular 2+ (bsmodal) use [config]="{ backdrop: 'static' }", .ts file like

import {ModalDirective} from 'ngx-bootstrap/modal';
@ViewChild('myModal') public myModal: ModalDirective;

 openpopup(){
     this.myModal.show()
  }

and .html like

<button class="btn btn-md btn-pill btn-success mb-3 pull-right" type="button" data-toggle="modal" (click)="openpopup()">Open</button>

 <div bsModal #myModal="bs-modal" class="modal fade"  [config]="{ backdrop: 'static' }"  tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">Popup heading</h4>
          <button type="button" class="close" (click)="myModal.hide()" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>

        <div class="modal-body">
          <!---- popup content here---->
        </div>
      </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
  </div><!-- /.modal -->
YasirPoongadan
  • 683
  • 6
  • 19
3

In your component.ts file

import { BsModalService, BsModalRef } from 'ngx-bootstrap/modal';
import { ViewChild, TemplateRef } from '@angular/core';

//--------

constructor(
    private modalService: BsModalService
  ) {}

@ViewChild('sampleModal', {static: true}) sampleModalRef: TemplateRef<any>;

modalRef: BsModalRef;

config = {
    backdrop: true,
    ignoreBackdropClick: true
  };

//-------

open() {
    this.modalRef = this.modalService.show(this.sampleModalRef, this.config);
  }

HTML

<ng-template #sampleModal>
      .
      .
      .
      .
</ng-template>

Mohit Kumar
  • 144
  • 2
  • 4