Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function on confirm not working #103

Closed
stefanoschrs opened this issue Oct 10, 2014 · 9 comments
Closed

Function on confirm not working #103

stefanoschrs opened this issue Oct 10, 2014 · 9 comments

Comments

@stefanoschrs
Copy link

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?

@ruarfff
Copy link

ruarfff commented Oct 10, 2014

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: #52

@shmeeps
Copy link

shmeeps commented Oct 12, 2014

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.

@stefanoschrs
Copy link
Author

Thanks but I have already tried that and it's doing nothing

@oitozero
Copy link

@schris12 check ngSweetAlert. maybe it can help

@blaiprat
Copy link

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);

@SeanPlusPlus
Copy link

@oitozero - ngSweetAlert works like a charm! thanks!

@ruarfff
Copy link

ruarfff commented Oct 14, 2014

@oitozero, ngSweetAlert worked for me too. Thanks!
Thank you @blaiprat too for the tip and helping understand the issue.

@oitozero
Copy link

@SeanPlusPlus @ruairitobrien
Awesome. 👍

Just released a new version to reflect changes in sweetalert (0.2.0).

@t4t5 t4t5 closed this as completed Jan 19, 2015
@borodatych
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants