Reputation: 2879
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?
Upvotes: 2
Views: 644
Reputation: 364687
As mentioned in the comments above, use
<a href="" ng-click="logOut()">Logout</a>
rather than
<a href="#" ....
Upvotes: 1