In my Angular 4 app, let's assume that I'm inside a service.
At some point I want to ask to the user for a confirmation, currently I'm doing it with just a confirm(...)
request:
const result = confirm('Are you sure?');
what if instead I would like to show a ngx-bootstrap
modal with, let's say, two buttons "Yes" or "No" and obtain a similar result?
EDIT: in my case, I solved my issue by playing with Subjects. Here you can find my solution, in case it can be useful for someone else. However that solution does not solve this question which is about returning a value from a modal, so I leave it open.
Try like this :
myexample it's working correctly. hope this will help you
home.module.ts
home.component.html
home.component.ts
home-modal.component.html
home-modal.component.ts
Try with below option which is working for me. callbackOnModelWindowClose is the return value.
@ShinDarth You can add this function in your service and call this funcion whenever required.
In your Service, create this function
In your demo-modal.component.ts, create an EventEmitter
I hope this would help you
I used the solution from @Chandru, however to return a
true
orfalse
, instead of:I simply used:
I understand that most of the answers above are completely valid, but that the main goal is be able to invoke the confirmation dialog in this way...
Note that this is mostly syntactic sugar to simplify the use of a promise and the asynchronous stuff.
I think it's what the OP was looking for and probably can be reworked to support returning any other data type (apart from a boolean).
The rest of the code below (not including the template to keep this short), pretty straightforward..
ModalConfirmationService
ModalConfirmationComponent