Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically close ng-bootstrap modal?

I've got a modal:

<template #warningModal let-c="close" let-d="dismiss">   <div class="modal-header">     <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">       <span aria-hidden="true">&times;</span>     </button>     <h4 class="modal-title">Warning</h4>   </div>   <div class="modal-body">       The numbers you have entered result in negative expenses.  We will treat this as $0.00.  Do you want to continue?   </div>   <div class="modal-footer">     <button type="button" class="btn btn-secondary" (click)="c('Close click')">No</button>     <button type="button" class="btn btn-secondary" (click)="submit()">Yes</button>   </div> </template> 

Whenever I click yes, I want it to call a function and close itself.
In my controller, I have @ViewChild('warningModal') warning; and in submit(), I have this.warning.close();, but I get this.warning.close is not a function whenever I click Yes.

How do I get this to work the way I want it to?

like image 525
Alex Kibler Avatar asked Nov 02 '16 14:11

Alex Kibler


People also ask

How do I close a Bootstrap modal?

Clicking on the modal “backdrop” will automatically close the modal. Bootstrap only supports one modal window at a time.

How do you automatically close a modal?

Inside the Open event handler of the jQuery UI Dialog Modal Popup box, using the JavaScript setTimeout function and the jQuery UI Dialog “close” command, the jQuery UI Dialog Modal Popup box is set to automatically close after delay of 5 seconds (some seconds). This dialog will automatically close in 5 seconds.

How do I close a Bootstrap modal in angular 8?

Opening & Closing Angular 8 Modal Dialogs To close a modal call the modalService. close() method with the id of the modal you want to close, e.g. modalService. close('custom-modal-1') .

How do I stop a Bootstrap modal from closing?

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.


1 Answers

To expound upon pkozlowski.opensource's response, the way I actually got the reference to the NgbModalRef class was by modifying what is in his plunker on the this.modalService.open as follows:

this.modalReference = this.modalService.open(content); this.modalReference.result.then((result) => {   this.closeResult = `Closed with: ${result}`; }, (reason) => {   this.closeResult = `Dismissed ${this.getDismissReason(reason)}`; }); 

Then I was able to call:

this.modalReference.close(); 

Which worked like a charm. Oh, and don't forget to put define modalReference at the top of the class:

modalReference: any; 
like image 112
TBrenner Avatar answered Oct 04 '22 09:10

TBrenner