我使用组件作为模板打开一个模型,一切正常,模态打开,我订阅onHide事件,订阅工作也。
但我在这里有一个挑战,我想发送一个具体的理由,例如:“消息成功添加”作为理由。我怎么能这么做?我如何发送特定的字符串作为理由?
目前,我正试图在MessageAddComponent组件中设置一个值,并使用bsModalRef.Content在父组件中访问它,但这不是一个好主意。
newMessage() {
this.bsModalRef = this.modalService.show(MessageAddComponent, {
class: 'modal-lg'
});
this.subscriptions.push(this.modalService.onHide.subscribe((reason: string) => {
// i dont like this approach
if (this.bsModalRef.content.anySuccessfulAction) {
console.log('foo and bar')
}
this.unsubscribe();
}));
}
发布于 2017-11-13 00:56:24
最后找到解决办法:
在用作模态的组件中注入BsModalService,然后将拒绝原因设置为
this.modalService.setDismissReason(theReason);
发布于 2019-07-26 05:59:34
为了简化订阅,您可以通过.take()或first()操作符创建“一次性”订阅:
this.modalService.onHide
.pipe(take(1))
.subscribe(() => {
console.log(this.bsModalRef.content)
});
发布于 2019-11-18 07:03:12
好的解决办法综合起来:
this.modalService.onHide.pipe(take(1), filter(reason => reason === 'yourReason')).subscribe(() => {
// Do anything here, this will get called only once
});
然后在你的模式中隐藏你的模式:
this.modalService.setDismissReason('yourReason');
https://stackoverflow.com/questions/47259536
复制相似问题