2016-07-29 93 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