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...
async openModalConfirmation() {
const result = await this.confirmationSvc.confirm('Confirm this...');
if (result) {
console.log('Yes!');
} else {
console.log('Oh no...');
}
}
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
import { ModalConfirmationComponent } from './component';
@Injectable()
export class ModalConfirmationService {
constructor(private bsModalService: BsModalService) {}
confirm(message: string): Promise<boolean> {
const modal = this.bsModalService.show(ModalConfirmationComponent, { initialState: { message: message }});
return new Promise<boolean>((resolve, reject) => modal.content.result.subscribe((result) => resolve(result) ));
}
}
ModalConfirmationComponent
import { Component, Input, Output, EventEmitter} from '@angular/core';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import { Subject } from 'rxjs/Subject';
@Component({
templateUrl: './component.html'
})
export class ModalConfirmationComponent {
@Input() message: string;
result: Subject<boolean> = new Subject<boolean>();
constructor(public modalRef: BsModalRef) { }
confirm(): void {
this.result.next(true);
this.modalRef.hide();
}
decline(): void {
this.result.next(false);
this.modalRef.hide();
}
}
this.modalService.show(this.confirmModal)
but I don't know how to receive back the result of what the user clicked in the modal – Tankage