How can I make the primng confirmation service behave equal to the browser native confirm?
When a user clicks on a button, I need under certain conditions to request their confirmation to proceed, in case of denying it, exit the method.
The following is an example using the primeng confirmation service. The problem is that the last line of the method that calls openFile is executed without waiting for user approval.
confirm(): void {
if (this.condition) {
this.confirmationService.confirm({
message: 'Are you sure that you want to proceed?',
header: 'Confirmation',
icon: 'pi pi-exclamation-triangle',
accept: () => {
this.openFile();
this.messageService.add({
severity: 'info',
summary: 'Confirmed',
detail: 'You have accepted'
});
return;
},
reject: () => {
return;
}
});
}
this.openFile();
}
On the other hand by implementing the same logic using the browser native confirmation. It works as expected, so that in the proper condition it will wait for the user's confirmation
confirm1(): void {
if (this.condition) {
const result = confirm('Are you sure that you want to proceed?');
if (result) {
this.openFile();
return;
} else {
return;
}
}
this.openFile();
}
The openFile
methodo has a simple console log
openFile(): void {
console.log('Some file');
}
How to get the confirmation service of primeng wait for the approval or denial of the user?
You can interact with an example of the behavior described in this repository https://github.com/ilmoralito/sample-primeng-confirmation-service-vs-native-confirmation