I\'ve got a modal:
You have let-d
or let-c
in <template #warningModal let-c="close" let-d="dismiss">
as variables, both are functions. So, the simple way to do it is: pass in c
or d
into submit as argument like this (click)="submit(c)"
or (click)="submit(d)"
and then in the function just call submit(c){ c('close modal'); }
. Hope that works for you.
If you are using https://ng-bootstrap.github.io/ (as indicated in your question) things are extremely simple - you can just open a modal and either close it from a template (as in your code) or programmatically (by calling close()
method on the returned Object of type NgbModalRef
).
Here is a minimal example showing this in action: http://plnkr.co/edit/r7watmQuyaUQ8onY17Z1?p=preview
You might be either confusing different libraries or maybe there is sth more to your question but it is hard to say more based just on the info provided.
What you've done with @ViewChild('warningModal') warning
is get the TemplateRef you used as in your modal, not the actual NgbModalRef
.
It depends how you open up your modal, if you open it programmatically you should receive the NgbModalRef
object which you could call .close
on.
You can simply close the modal by below way.
First when we open the Modal
this.modalService.open(content, { size: "lg" }).result.then(
result => {
this.closeResult = `Closed with: ${result}`;
},
reason => {
this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
}
After That in TS For closing use this
this.modalService.dismissAll();
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;
To open the modal
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
modalReference = null;
constructor(private modalService: NgbModal) {
}
openVerticallyCentered(content) {
this.modalReference = this.modalService.open(content, { centered: true });
}
to close the modal using the reference, like Amit said
this.modalReference.close();
source:https://ng-bootstrap.github.io/#/components/modal/examples