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?

turbo2oh
  • 2,849
  • 6
  • 34
  • 46
  • Probably a typo, but just in case you didn't notice... in logOut() you set the path to `/logout` instead of `/login`. – Mark Rajcok Aug 15 '13 at 01: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. – turbo2oh Aug 15 '13 at 01:39
  • Okay, my mistake. Is 'logging out' being logged to the console? – Mark Rajcok Aug 15 '13 at 02:39
  • yep, it is. its logging out the user. – turbo2oh Aug 15 '13 at 02:43
  • Is it only the logout route that isn't working? – Sharondio Aug 15 '13 at 03:57
  • Its not the logout route thats the issue. For example I changed the $location.path after logout to go to login and it still didn't work. Its how/where I'm calling it after logout that's the issue. Is it because its being called from the index.html instead of inside a ng-view? – turbo2oh Aug 15 '13 at 13:15
  • That shouldn't matter. Can you set up a plunker or a fiddle? – Mark Rajcok Aug 15 '13 at 15:15
  • So I gave the plunkr a shot but I don't think you can get it to work with routing since its not working in html5 hashbang mode. It always hits the 'otherwise' route: http://plnkr.co/edit/lWPtxY0cXKEY90BCmUE5 – turbo2oh Aug 15 '13 at 20:35
  • 1
    Using `Logout` worked in the plunker, instead of ` – Mark Rajcok Aug 15 '13 at 22:11
  • removing the # solved it! I'll have to read up on the differences in that link, thanks for that, if you add this as an answer ill mark it fixed. – turbo2oh Aug 16 '13 at 00:36

1 Answers1

1

As mentioned in the comments above, use

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

rather than

<a href="#" .... 
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492