ng-bootstrap 模态在交叉点击时不关闭

ng-bootstrap modal not closing on cross click

我正在使用 ng-bootstrap 模态弹出窗口,它不会在单击十字按钮时关闭。

这是触发弹出窗口的 <a> 标记 -

<div class="actions padding-zero">
    <a href="javascript:void(0);" (click)="openFilter()" class="icon configure-columns-icon">
        <span class="control-label">Configure Columns</span>
    </a>
</div>

模态 -​​

<ng-template #filterForm let-modal>
    <div class="TitlePanel">
    Configure Columns                       
    <button type="button" class="close" aria-label="Close" (click)="modal.dismiss('Cross click')">
    X
    </button>
    </div>
    <div class="modal-body">
      Body
    </div>                   
</ng-template>

component.ts 文件 -

export class NgbdModalContent {
    @Input() name;
    constructor(public activeModal: NgbActiveModal) { }
}


@Component({
    selector: 'app-modals',
    templateUrl: './modals.component.html',
    styleUrls: ['./modals.component.scss'],
    encapsulation: ViewEncapsulation.None,
})

export class ModalsComponent {

    closeResult: string;

    constructor(private modalService: NgbModal) { }

    // Open default modal
    open(content) {
        this.modalService.open(content).result.then((result) => {
            this.closeResult = `Closed with: ${result}`;
        }, (reason) => {
            this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
        });
    }

    // This function is used in open
    private getDismissReason(reason: any): string {
        if (reason === ModalDismissReasons.ESC) {
            return 'by pressing ESC';
        } else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
            return 'by clicking on a backdrop';
        } else {
            return `with: ${reason}`;
        }
    }

    // Open modal with dark section
    openModal(customContent) {
        this.modalService.open(customContent, { windowClass: 'dark-modal' });
    }

    // Open content with dark section
    openContent() {
        const modalRef = this.modalService.open(NgbdModalContent);
        modalRef.componentInstance.name = 'World';
    }
}

此外,当我单击关闭按钮时,我在控制台中收到此错误 - "Cannot read property 'dismiss' of undefined"

您正在使用 modal 但您正在注入 activeModal。将 html 代码更新为:

<ng-template #filterForm let-modal>
    <div class="TitlePanel">
    Configure Columns                       
    <button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
    X
    </button>
    </div>
    <div class="modal-body">
      Body
    </div>                   
</ng-template>
(click)="modal.dismiss('Cross click')"

需要改为

(click)="activeModal.dismiss('Cross click')"

因为你在给予

public activeModal: NgbActiveModal

经过一番研究并进行了这些更改后,它奏效了。

<ng-template #filterForm let-d="dismiss">
    <div class="TitlePanel">
        Configure Columns                       
        <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
        X
        </button>
    </div>
    <div class="modal-body">
       Body
    </div>                   
</ng-template>
[solution by   using modal reference][1]

[1]: 有效