1

I have this navigation:

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

But when I click the logout link, nothing happens.

Here is my controller:

'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);

And here is the configured route:

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

I am 100% new to AngularJS, so please excuse my ignorance. Thanks!

3
  • you are treating the view variables as if they are part of $scope but controller is not configured for $scope but for controllerAs in view. How are controllers assigned to view? Commented Jul 19, 2015 at 2:21
  • I changed my controller option in the route to be LoginCtrl as LoginCtrl and it's still not working. I'm not sure if that's what you mean Commented Jul 19, 2015 at 2:23
  • 1
    that means you need to prefix all the controller variables in view with that alias and a dot Commented Jul 19, 2015 at 2:25

1 Answer 1

1

the view you use is configured for $scope ,, yet your controller uses controller alias ,, so you can use this line to make the button work

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

UPDATE : notice that in ngClick you put the expression not the function so you put logout() not only logout

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, that worked!! I'm confused, however. What is the difference from doing the controller alias here vs doing it in the route config?
controller already assigned in routing...will run 2 instances this way
I think the routing instance will be intialised only when he navigates to /logout , yet here he intialise the controller in the dropdown menu in the navigation bar not only in /logout !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.