I am just starting to learn Angular.
Currently, I have an application that contains two variables related to the status of financial transactions. These variables are: tab1TrxMessage
, which holds any important messages, and tab1TrxStatus
that indicates whether a transaction has Failed, is successful, or is in pending status. In case of a failed transaction, I want the user to receive a swal alert/notification like the one below:
swal({
title:'Error!',
text: 'Warning... Transaction failed!',
type: 'error',
confirmButtonText: 'Ok',
confirmButtonColor: '#ff754f'
});
My current code snippet looks like this:
../component.html
<div class="sign-btn text-center">
<a class="btn btn-lg text-white">
<span *ngIf="tab1TrxMessage">{{tab1TrxMessage}}</span>
<span *ngIf="!tab1TrxMessage && tab1TrxStatus != 'Success'">Failed</span>
<span *ngIf="transactionFailed()">Failed</span>
</a>
</div>
Focusing on the transactionFailed()
function...
../component.ts
public transactionFailed() {
if(this.tab1TrxMessage == 'Failed: Cancelled by User' && this.tab1TrxStatus != 'Success') {
console.log("You are in transactionFailed!")
swal({
title:'Error!',
text: 'Warning... Transaction failed!',
type: 'error',
confirmButtonText: 'Ok',
confirmButtonColor: '#ff754f'
});
return true;
}
return false;
}
After a failed transaction using the above code, the message You are in transactionFailed!
appears multiple times in the browser console...
https://i.sstatic.net/Qs7Od.png
...and the swal alert with Warning... Transaction failed!
keeps popping up repeatedly. This situation leads me to wonder if there's a better way to ensure that the swal alert displays only ONCE per failed transaction?
I would appreciate your suggestions and guidance.