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">×</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?
Clicking on the modal “backdrop” will automatically close the modal. Bootstrap only supports one modal window at a time.
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.
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') .
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.
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;
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