这个问题通常是由于未正确关闭先前的模态窗口导致的。为了解决这个问题,可以在打开模态窗口之前先检查是否有先前打开的模态窗口,如果存在,则需要先销毁该模态窗口,然后再打开新的模态窗口。
以下是一个示例代码,演示如何解决这个问题:
import { Component, OnDestroy } from '@angular/core';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'app-example',
template: `
`
})
export class ExampleComponent implements OnDestroy {
activeModal = null;
constructor(private modalService: NgbModal) {}
openModal() {
if (this.activeModal) {
this.activeModal.dismiss('销毁上一个模态窗口');
}
this.activeModal = this.modalService.open('template-modal');
}
ngOnDestroy() {
if (this.activeModal) {
this.activeModal.dismiss('销毁上一个模态窗口');
}
}
}