拥有身份myModal
的模式我试图在事件显示和关闭时连接到事件。在https://v4-alpha.getbootstrap.com/components/modal/#events的文档之后,我将以下内容添加到我的模态组件中:
this.modalElement = document.getElementById("myModal");
this.modalElement.addEventListener("hidden.bs.modal", this.onModalHidden);
this.modalElement.addEventListener("shown.bs.modal", this.onModalShown);
现在的事件处理程序将只有console.log"显示"或者"隐藏"测试,但不幸的是我不能让它工作:
private onModalShown = (event) => {
console.log("modal is shown!!!");
}
是否有遗漏的东西,是否有可能实现这一目标?
答案 0 :(得分:5)
您可以在ModalComponent中订阅onDismiss
和@ViewChild('yourModal')
yourModal: ModalComponent;
事件。
ngAfterViewInit
我建议您订阅ngAfterViewInit() {
this.yourModal.onOpen.subscribe(() => {
//do something
});
this.yourModal.onDismiss.subscribe(() => {
//do something
});
}
.htaccess
答案 1 :(得分:1)
您可以尝试以下代码段:
首先,我使用ViewChild指令来调用我的模态。
@ViewChild('MyModal') mymodal: ElementRef;
//show
$(this.mymodal.nativeElement).modal('show');
//hide
$(this.mymodal.nativeElement).modal('hide');
//catch the event when modal is hidden
//trigger implicity hide when backdrop is clicked or esc keypress
// i had issues on closing modals and this fixed the problem
$(this.mymodal.nativeElement).on('hidden.bs.modal', () => {
$(this).modal('hide');
this.cancelProcedure();//my internal reset function
});