It sounds like you want different behavior based on if the user presses the confirm button or the cancel button.
SweetAlert exposes the user response via a parameter on the callback function.
Here's an example straight form the SweetAlert Docs:
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm) {
if (isConfirm) {
swal("Deleted!", "Your imaginary file has been deleted.", "success");
} else {
swal("Cancelled", "Your imaginary file is safe :)", "error");
}
}
);
In this example, when the confirm button is pressed, a new SweetAlert is opened, confirming your action, and if cancel button is pressed, a new SweetAlert is opened, noting that the action was cancelled. You can replace these calls with whatever functionality you need.
Since this library is using async callbacks, there is not a return value from the swal
method.
Also, it would probably be good idea to use a library such as ng-sweet-alert to wrap calls to sweet alert to ensure that any changes you make in Sweet Alert callbacks are properly picked up by the angular lifecycle. If you check out the source for ng-sweet-alert, you'll see that the author wraps the calls to swal
and the user's callback in $rootScope.$evalAsync
, ensuring that angular updates when the calls and callbacks complete.
From a code style perspective, it would be better to put your logic into a service or factory for reuse throughout your codebase rather than just attaching it to $rootScope.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…