I am opening a modal using component as a template, everything is ok, modal opens and I am subscribing to onHide Event, subscription works also.
but I have a challenge here, I want to send a specific reason for example: 'message added successfully' as the reason . how can I do that? how can I send a specific string as a reason?
currently, I am trying to set a value in MessageAddComponent component and access it in parent component using bsModalRef.Content, but it is not a good idea.
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();
}));
}
To simplify your subscription you can create "onetime" subscription via .take() or first() operator:
this.modalService.onHide
.pipe(take(1))
.subscribe(() => {
console.log(this.bsModalRef.content)
});
found solution finally:
inject BsModalService in the component that is used as modal and then set dismiss reason as bellow
this.modalService.setDismissReason(theReason);
Good solution put together:
this.modalService.onHide.pipe(take(1), filter(reason => reason === 'yourReason')).subscribe(() => {
// Do anything here, this will get called only once
});
And then in your modal before hiding your modal:
this.modalService.setDismissReason('yourReason');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With