angular – 如果在外部单击或ESC按钮,则限制ng-bootstrap模式关闭

我正在使用ng-bootstrap为我的angular2项目打开一个模态.如果我点击外面和ESC按钮,模态会解散.我想限制这两种行为.下面是我打开模态的代码

import { NgbModal, NgbModalOptions } from '@ng-bootstrap/ng-bootstrap';
.....
modalOption: NgbModalOptions = null;
constructor(
private modalService: NgbModal,
....
}
....
//the method I am using to open a modal
openAddModal() {
    this.modalOption.backdrop = 'static';
    this.modalOption.keyboard = false;
    const modalRef =  this.modalService.open(PremiumProtectionComponent,this.modalOption);
    .....
}

PremiumProtectionComponent是模态的ts文件.

下面是我调用该方法的HTML

< a(click)=“openAddModal()”>点击打开模态< / a>

任何人都可以建议我实现这种行为的正确方法.提前致谢

最佳答案
您需要在modalOptions对象上设置背景和键盘属性,而不是null:

modalOption: NgbModalOptions = {}; // not null!

// ...

openAddModal() {
    this.modalOption.backdrop = 'static';
    this.modalOption.keyboard = false;
    const modalRef = this.modalService.open(PremiumProtectionComponent,this.modalOption);
}

转载注明原文:angular – 如果在外部单击或ESC按钮,则限制ng-bootstrap模式关闭 - 代码日志