stop closing the modal by clicking backdrop or outside the modal
Solution 1
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.
Solution 2
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.
Solution 3
$modal.open({
// ... other options
backdrop : 'static',
keyboard : false
});
While you creating your modal you can specify its behavior:
Solution 4
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">×</span>
</button>
</div>
<div class="modal-body">
<!---- popup content here---->
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
Solution 5
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>
Comments
-
Partha Sarathi Ghosh almost 2 years
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 over 7 yearsIs it Angular2 code? I don't think so. I am expecting Angular2 code.
-
pkozlowski.opensource over 7 yearsYes, this is Angular code (in fact it is just JavaScript as options to ng-bootstrap modal service are just an object literal). As the author of this component I can also say that it is a correct answer and should be accepted. Good job @anshuVersatile
-
Partha Sarathi Ghosh over 7 yearsI am using TypeScript and Angular2 here, not JavaScript.
-
Chris Halcrow almost 6 yearsNote that there's a similar, but completely independent angular bootstrap library 'ngx-bootstrap'. The options you need to set are different for this - see valor-software.com/ngx-bootstrap/#/…. You'll need
let config = { backdrop: true, ignoreBackdropClick: true };
then ...this.modalService.show(SomeComponent, config);
-
Thilina Sampath almost 6 yearsloading... in plunker => Error {originalErr: Error, constructor: Object}
-
SHUBHASIS MAHATA about 4 yearsWorking with ngx-bootstrap with angular 9
-
Sarath Mohandas over 3 yearsI take it as answer. [config]="{ backdrop: 'static' }"