0

In my current project, for logout I want to add confirmation. For the confirmation I want sweet alert. I found solution for confirmation and sweet alert separately but I am finding it difficult to combine those two. HTML:

<a href="javascript:void(0);" ng-click="Logout()"><i class="fa fa-sign-out" aria-hidden="true"></i> Logout</a>

js:

app.controller('LogoutCtrl', ['$scope', '$location', 'localStorageService', function($scope, $location, localStorageService) {
    $scope.Logout = function() {
        swal({
                title: "Are you sure to logout?",
                text: "",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: '#DD6B55',
                confirmButtonText: 'Yes',
                cancelButtonText: "No",
                closeOnConfirm: false,
                closeOnCancel: false
            },
            function(isConfirm) {
                if (isConfirm) {
                    localStorageService.clearAll();
        $location.path('/login');

                }
            });
    }
}]);

I know there can't be function inside another function but I can't find other way round. Instead of angular confirm I want to use sweet alert. Is it possible. Thanks in advance.

Code in action

samjhana joshi
  • 1,995
  • 4
  • 35
  • 69

2 Answers2

0

there can be a function inside another function in javascript

function(){
       function abc(){
       }


       //use abc() as much as you want               
}
jayanthCoder
  • 615
  • 1
  • 6
  • 13
  • I am not abkle to understand your problem statement ... can you pls be more specific – jayanthCoder Aug 23 '16 at 06:52
  • Yes I know there can't be another function inside a funciton. I am searching a workaround for it. – samjhana joshi Aug 23 '16 at 06:56
  • I am using the same. Problem is I can't get angular and swal alert to work on confirmation. When user clicks on logout I want swal alert as confirmation box. Only is user confirms then user be logged out. The problem with my work is there can't be function inside a function. – samjhana joshi Aug 23 '16 at 07:00
0

You have special case, where you are making changes in location path without letting angular know that something got change, eventually digest cycle isn't getting fire. You should fire digest cycle manually for making location changes to understand by angular router. For running digest cycle you could use $timeout(inject inside controller before using it)

var ctrl = function($scope, $location,$timeout) {

  swal({
      title: "Are you sure to logout?",
      text: "",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: '#DD6B55',
      confirmButtonText: 'Yes',
      cancelButtonText: "No",
      closeOnConfirm: true, //it should be true to close popup on confirm
      closeOnCancel: true  //it should be true to close popup on cancel
    },
    function(isConfirm) {
      if (isConfirm) {
        $timeout(function(){
          localStorageService.clearAll();
          $location.path('/login');          
        });
      }
    });
}

Code In Action


You could look at this answer as well, to implement swal alert in angular, which will internally handle digest cycle thing internally.

Community
  • 1
  • 1
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299