Hello, I have used SweetAlert before with success but today I tried using it with Ionic and I have a problem.
When I use it like this it executes the something function but it's not waiting for the confirm button to be pressed:
$scope.something=function(){console.log('test')};
swal({title: 'True',text: '',type: 'success',confirmButtonText: 'Next' },$scope.something());
When I use it like this, it's not working at all:
swal({title: 'True',text: '',type: 'success',confirmButtonText: 'Next' },function(){console.log('test')});
Any ideas?
I've come across something similar in angular.
$scope.deleteSomething = function () {
swal({ title: 'Are you sure?', text: 'Your will not be able to recover this thing!',
type: 'warning',
showCancelButton: true, confirmButtonColor: '#DD6B55',
confirmButtonText: 'Yes, delete it!', closeOnConfirm: false },
function () {
// Code to delete something
swal('Deleted!', 'It was deleted.', 'success');
// The success alert only shows for a split second and is gone
}
});
Seems to be something to do with having it in a function called in the scope or just having it in a callbak but what do I know :)
If I figure it out I'll post here. Looks like maybe something similar going on here: https://github.com/t4t5/sweetalert/issues/52
Might be mistaken, but it looks like you need to remove the parens on $scope.something() as the call back, like
swal({title: 'True',text: '',type: 'success',confirmButtonText: 'Next' },$scope.something);
When you include the parens, the JS interpreter is executing $scope.something() and assigning the result as the callback, hence why it's not waiting for the confirm button to be pressed. In your second example you're passing the function definition in (same as just passing in $scope.something without parens) which is why that's working.
Thanks but I have already tried that and it's doing nothing
@schris12 check ngSweetAlert. maybe it can help
The console log should work, but just in case. Remember that Angular you need to call $scope.$digest(); if you're using other libraries to update views & scopes.
So if you're using sweet alert, make sure your callback looks like:
$scope.something=function(){
console.log('test');
// You might have something that updates your scope here
...
$scope.$digest();
};
swal({
title: 'True',
text: '',
type: 'success',
confirmButtonText: 'Next'
}, $scope.something);
@oitozero - ngSweetAlert works like a charm! thanks!
@oitozero, ngSweetAlert worked for me too. Thanks!
Thank you @blaiprat too for the tip and helping understand the issue.
@SeanPlusPlus @ruairitobrien
Awesome. :+1:
Just released a new version to reflect changes in sweetalert (0.2.0).
Many thanks.
There is little, or even a big "but" - if you call in the active modeling window, the buttons are not working.
Please tell me how to get rid of the scourge?
Most helpful comment
The console log should work, but just in case. Remember that Angular you need to call $scope.$digest(); if you're using other libraries to update views & scopes.
So if you're using sweet alert, make sure your callback looks like: