AngularJS ng-click不使用配置的Controller

时间:2015-07-19 02:16:46

标签: angularjs

我有这个导航:

                    <ul class="dropdown-menu m-t-xs compact" style="float:right !important;right: 0px;">
                        <li ng-hide="$state.includes('root.projects')"><a ui-sref="portal.user_settings"><span class="icon glyphicon glyphicon-user"></span>User Settings</a></li>
                        <li ng-show="$state.includes('root.projects')"><a ui-sref="root.user_settings"><span class="icon glyphicon glyphicon-user"></span>User Settings</a></li>
                        <li class="divider"></li>
                        <li ng-controller="LoginCtrl"><a href="" ng-click="logout"><span class="icon glyphicon glyphicon-log-out"></span>Logout</a></li>
                    </ul>

但是当我点击logout链接时,没有任何反应。

这是我的控制器:

'use strict';

function LoginCtrl($scope, Authentication, $window, $cookies, $http, $location) {
    var _self = this;
    _self.loginProcessing = false; //This is a flag for showing the loader animation while the wait for the login response.
    _self.error = null;

    _self.login = function(vm) {  //Defines the login function as a variable on the LoginCtrl
        _self.loginProcessing = true;
        Authentication.Login(vm.email, vm.password).then(function(data){
            _self.loginProcessing = false;

            $window.sessionStorage['currentUser'] = JSON.stringify(data);
            $location.path("/projects");
        }).catch(function(err) {
            _self.error = true;
            _self.loginProcessing = false;
        });
    }

    _self.logout = function() {
        alert("CLICKED");
        // Authentication.logout().then(function() {
        //  $window.sessionStorage['currentUser'] = null;
        //
        //  $location.path("/login");
        // });
    }
}

angular.module('controllers').controller('LoginCtrl', LoginCtrl);

这是配置的路线:

.state('logout',{
    url: "/logout",
    controller: "LoginCtrl",
    templateUrl: 'views/logout.html',
})

我是AngularJS的100%新手,所以请原谅我的无知。谢谢!

1 个答案:

答案 0 :(得分:1)

您使用的视图是为$ scope配置的,但您的控制器使用控制器别名,, 所以你可以使用这一行来使按钮工作

<li ng-controller="LoginCtrl as loginCtrl"><a href="" ng-click="loginCtrl.logout()"><span class="icon glyphicon glyphicon-log-out"></span>Logout</a></li>

更新:请注意,在ngClick中,您将表达式设置为不是函数,因此您不仅注销 注销