Reputation: 562
I'm having problems with redirect in angularJS.
For example it works perfectly fine, when the user logs in. If a user is logged in (information stored in the localstorage) it redirects him automatically to my '/dashboard'.
But when the user logs out it is not redirecting correctly but goes to 'http://localhost/ngJS/index.php#' instead of anything else I tell him. What do I do wrong?
ngJS is my project-root-folder and index.php is just a container with my libraries and code-inclusions. I am running the sites on a local server XAMMP.
var app = angular.module('mainApp', ['ngRoute']);
/*
* Configuration for redirecting
* @param {type} $routeProvider
*/
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
//WORKING
.when('/', {resolve: {
"check": function ($location) {
if (localStorage.getItem('usr') &&
localStorage.getItem('usr') !== '0') {
$location.path('/dashboard');
}
}
},templateUrl: 'pages/cover.html'
})
//WORKING LOADING THE CONTROLLER SUCCESSFULLY
.when('/dashboard', {
resolve: {
"check": function ($location) {
if (localStorage.getItem('usr') === '0') {
$location.path('/');
}}},
templateUrl: 'pages/dummy.html',
controller: 'logoutCtrl'
})
.when('/login', {
templateUrl: 'pages/login/login.html',
controller: 'loginCtrl'
})
.when('/logout', {redirectTo: '/'})
.otherwise({redirectTo: '/'});
}]);
app.controller('logoutCtrl', function ($scope, $rootScope, $location, $log)
{
$log.log('logoutCtrl');
$log.log($scope);
$scope.message = $rootScope.message;
$scope.logout = function () {
if (typeof (localStorage) !== 'undefined') {
localStorage.setItem('usr', 0);
// NOT WORKING --------------------- redirect
$location.path('/');
// WORKING AND PRINTING ---------------------
console.log('redirecting!');
}
/* No localstorage support */
else {
alert("Tut uns leid. Localstorage wird nicht unterstützt");
}
};
});
<section class="bg-success text-info" id="horizontal-nav-pills" >
<nav>
<ul class="nav nav-pills nav-justified">
<li role="presentation" class="active"><a href="#">Home</a></li>
<li role="presentation"><a href="#/new-report"><span class="glyphicon glyphicon-plus"></span> Bericht</a></li>
<li role="presentation"><a href="#"><span class="glyphicon glyphicon-chevron-left"></span></a></li>
<li role="presentation"><a href="#"><span class="glyphicon glyphicon-chevron-right"></span></a></li>
<li role="presentation" ><a href="#" ng-click="logout();">Logout</a></li>
</ul>
</nav>
</section>
<!--end horizontal-nav-pills-->
Upvotes: 1
Views: 112
Reputation: 26
Instead of using:
<a href="#" ng-click="logout();">Logout</a>
use:
<a href="javascript:void(0);" ng-click="logout();">Logout</a>
Upvotes: 1
Reputation: 7752
Since neither ngJS
nor index.php
is in the code you posted I suppose that the problem comes from a different file that is currently not on your radar.
I suggest to get an editor that support's searching in mutliple files (like Notepad++) and search through the project directory for the above mentioned, two terms.
Upvotes: 0
Reputation: 1709
$scope.logout = function () {
if (typeof (localStorage) !== 'undefined') {
localStorage.setItem('usr', 0);
// NOT WORKING --------------------- redirect
$location.path('/');
// WORKING AND PRINTING ---------------------
console.log('redirecting!');
}
/* No localstorage support */
else {
alert("Tut uns leid. Localstorage wird nicht unterstützt");
}
};
The problem is with:
$location.path('/');
It should have been:
$location.path('/logout');
Upvotes: 0