问题
I want to call (click)="basicModal.hide()" methode of MDBootstrap Modal inside one of my fuctions in my typescript component. I don't know how to access this method from component.
<button type="button" mdbBtn color="primary" class="relative waves-light" (click)="basicModal.show()" mdbWavesEffect>Launch demo modal</button>
<div mdbModal #basicModal="mdbModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myBasicModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close pull-right" aria-label="Close" (click)="basicModal.hide()">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title w-100" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" mdbBtn color="secondary" class="waves-light" aria-label="Close" (click)="basicModal.hide()" mdbWavesEffect>Close</button>
<button type="button" mdbBtn color="primary" class="relative waves-light" mdbWavesEffect>Save changes</button>
</div>
</div>
</div>
</div>
回答1:
You have to use the Angular's @ViewChild decorator to target the mdbModal directrive, and then use the hide() method from ModalDirective class.
Please take a look at the below code, there's explained everything:
.html:
<button type="button" mdbBtn color="primary" class="relative waves-light" (click)="basicModal.show()" mdbWavesEffect>Launch demo modal</button>
<div #modal mdbModal #basicModal="mdbModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myBasicModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close pull-right" aria-label="Close" (click)="basicModal.hide()">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title w-100" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<div class="md-form">
<input type="text" class="form-control" id="input1" mdbInput>
<label for="input1">Modal input</label>
</div>
</div>
<div class="modal-footer">
<button mdbBtn color="primary" mdbWavesEffect (click)="hideModal()">Hide modal</button>
</div>
</div>
</div>
</div>
.ts:
@ViewChild(ModalDirective) modal: ModalDirective;
hideModal() {
this.modal.hide();
}
If there will be something unclear, feel free to ask again!
来源:https://stackoverflow.com/questions/54973818/angular-7-hide-bootstrap-modal-in-typscript