2016-07-29 165 views
0

我已经尝试注销我当前的会话。但注销不起作用。检查下面我的代码:AngularJS注销不起作用

视图

<a ui-sref="logout"> 
    <i class="fa fa-sign-out"></i> Log out 
</a> 

config.js

$stateProvider 
      .state('logout', { 
       url: "/logout", 
       templateUrl: '', 
       controller: "LogoutController" 
      }); 

controllers.js

function LogoutController($location, $http) { 
    alert("hi"); 
    //Session.clear(); 
    var request = $http({ 
     method: "post", 
     url: "users/ajaxLogout", 
     dataType: 'json', 
     headers: {'Content-Type': 'application/x-www-form-urlencoded'} 
    }); 
    request.success(function(data) { 

    }); 
    $location.path('/login'); 
} 

angular 
    .module('inspinia') 
    .controller('MainCtrl', MainCtrl) 
    .controller('LogoutController', LogoutController); 

我已在LogoutController中发出提醒,但该功能在我点击Log out链接时未调用。

我关注此链接 - Angular - Logout redirect route请检查并告诉我我的代码中的错误在哪里。

+1

请参阅** [mcve] **。准确解释哪些不起作用。 – Tushar

+0

这里有什么问题? –

+0

控制台中是否有任何错误? –

回答

2

将注销代码函数内部在你的控制器:

function LogoutController($location, $http, $scope) { 

    this.logout = function() { 
     alert("hi"); 
     //Session.clear(); 
     var request = $http({ 
      method: "post", 
      url: "users/ajaxLogout", 
      dataType: 'json', 
      headers: {'Content-Type': 'application/x-www-form-urlencoded'} 
     }); 
     request.success(function(data) { 
      $location.path('/login'); 
     }); 

    } 
} 

然后调用点击退出链接功能通过使用ng-click:

<div ng-controller="LogoutController as ctrl"> 
<a ui-sref="logout"> 
    <i class="fa fa-sign-out" ng-click="ctrl.logout()"></i> Log out 
</a> 
</div> 
+0

您的代码正在工作。但我得到另一个错误。登录页面自动注销后无需点击注销链接。 :( – Chinmay235

+0

我不确定注销功能是否需要$ stateProvider,所以看看它是否会导致错误,另请参阅更新后的代码,我已将$ location.path('/ login');放在“request”下。成功()“功能 – Rishabh

+1

是的,这工作正常,谢谢。 – Chinmay235

-1

检查控制台被你得到一个错误,并尝试

angular .controller('LogoutController', function($scope,$state,$location,$ionicHistory, $http) { 
    console.log("hello"); 
    $state.go('app.login'); 
    $ionicHistory.clearCache(); 
    }; 
+0

如果您低估了它,请提及您的答案。 – Anuj