2

I have a navbar area that lives in my index.html file, the rest of the content is in partials inside a ng-view. Within the navbar it displays the user's username and has a logout link. Clicking the link logs out the user and I'd like to redirect them back to the login page.

Here's my logout method in my navBar controller:

 $scope.logOut = function () {
    console.log('logging out');
    StackmobService.logout();
    $location.path('/logout');
    $scope.currentUser='';
};

My router is as follows:

$routeProvider.
    when('/register', {templateUrl: 'partials/register.html',   controller: 'LoginCtrl'}).
    when('/login', {templateUrl: 'partials/login.html',   controller: 'LoginCtrl'}).
    when('/home', {templateUrl: 'partials/home.html',   controller: 'LoginCtrl'}).
    when('/events', {templateUrl: 'partials/events.html',   controller: 'EventCtrl'}).
    when('/logout', {templateUrl: 'partials/register.html',   controller: 'LoginCtrl'}).
    otherwise({redirectTo: '/home'});

Here's the body piece of my index.html file:

<div class="container">

    <div class="navbar" ng-controller="NavBarCtrl">
        <a class="navbar-brand" href="#">spreevent</a>
        <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#">Link</a></li>
        </ul>
        <p class="navbar-text pull-right" ng-show="currentUser.username">
            Welcome {{currentUser.username}}
            <a href="#"  ng-click="logOut()">Logout</a>
        </p>

    </div>

    <ng-view></ng-view>
</div>

Any idea why its not changing?

10
  • Probably a typo, but just in case you didn't notice... in logOut() you set the path to /logout instead of /login. Commented Aug 15, 2013 at 1:31
  • Well I should update my router to go to login.html instead of register.html for /logout. But if you look at my router, right now the /logout path should redirect to register.html and its not even doing that. its not redirecting at all. Commented Aug 15, 2013 at 1:39
  • Okay, my mistake. Is 'logging out' being logged to the console? Commented Aug 15, 2013 at 2:39
  • yep, it is. its logging out the user. Commented Aug 15, 2013 at 2:43
  • 1
    Using <a href="" ng-click="logOut()">Logout</a> worked in the plunker, instead of <a href="#" .... Does that solve your problem? If not what mode are you using in your app, HTML5, hashbang, or hashbang in HTML5 (see also stackoverflow.com/a/16678065/215945)? Commented Aug 15, 2013 at 22:11

1 Answer 1

1

As mentioned in the comments above, use

<a href="" ng-click="logOut()">Logout</a>

rather than

<a href="#" .... 
Sign up to request clarification or add additional context in comments.

Comments

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.