AngularJS注销无法正常工作

时间:2016-07-29 09:52:40

标签: javascript 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请检查并告诉我代码中的错误。

2 个答案:

答案 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>

答案 1 :(得分:-1)

检查控制台是否收到错误并尝试

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