gpt4 book ai didi

angular - ngbModal 作为通用确认框

转载 作者:太空狗 更新时间:2023-10-29 17:09:55 26 4
gpt4 key购买 nike

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

    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 promise。这就像在表示模态内容的组件中执行以下操作一样简单(注意 activeModal.close(...)):

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

之后您可以从NgbModalRefresult promise 中得到用户的回答。 (注意 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

关于angular - ngbModal 作为通用确认框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43611743/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com