ngbModal 作为通用确认框

ngbModal as a generic Confrimation Box

我正在尝试使用 ngbmodal 创建通用确认框,它将在整个应用程序中使用。其中,标题和消息将从调用组件传递到模式。我创建了一个 DialogService 并添加到 entryComponents 中。现在我可以显示确认框了。但是无法得到结果。下面是显示 ConfirmationBox 组件的代码。如何从中获取值

    const modalRef = this.modalService.open(ConfirmationBoxComponent,{backdrop:"static"})
    modalRef.componentInstance.name = "Message";
    modalRef.componentInstance.confirmationBoxTitle = "Confirmation?"
    modalRef.componentInstance.confirmationmessage = "Do you want to cancel?"
    modalRef.componentInstance.changeRef.markForCheck();

有许多简单的方法可以实现这一点,但我建议采用最简单、最有效的 IMO 方法:根据用户的选择解决模态的 result 承诺。这就像在表示模态内容的组件中执行以下操作一样简单(注意 activeModal.close(...)):

<button (click)="activeModal.close(true)">Yes</button>
<button (click)="activeModal.close(false)">No</button>

之后您可以从 NgbModalRefresult 承诺中取回用户的答案(通知 modalRef.result.then):

open() {
    const modalRef = this.modalService.open(NgbdModalContent);
    modalRef.componentInstance.confirmationBoxTitle = 'Confirmation?';
    modalRef.componentInstance.confirmationMessage = 'Do you want to cancel?';

    modalRef.result.then((userResponse) => {
      console.log(`User's choice: ${userResponse}`)
    });        
  }

您可以在以下插件中看到所有这些操作:http://plnkr.co/edit/yYIx1m1e2nfK0nxFwYLJ?p=preview