如果我在登录后复制欢迎页面的URL并在注销后粘贴欢迎页面的URL,它将重定向到登录页面,它正常工作,因为我正在检查本地存储名称。现在的问题是我无法进入注册页面,因为当用户注销新用户注册时应该打开它,因为用户在本地存储中将为空,请任何人帮我解决这个问题。
这是我申请的路线
app.config([ '$routeProvider', function($routeProvider) {
$routeProvider.when('/', {
templateUrl : 'app/components/login/login.html',
controller : 'loginCtrl'
}).when('/register/', {
templateUrl : 'app/components/register/register.html',
controller : 'registerController'
}).when('/welcome/', {
templateUrl : 'app/components/dashBoard/dashboard.html',
controller : 'welcomeController'
}).when('/logout', {
templateUrl : 'app/components/login/login.html',
controller : 'LogoutController'
}).when('/forgotPwd', {
templateUrl : 'app/components/forgotPassword/forgotPassword.html',
controller : 'forgotPwdController'
}).when('/changePwd', {
templateUrl : 'app/components/changePwd/changePassword.html',
controller : 'changePwdController'
}).otherwise({
redirectTo : "/"
});
} ]).run(function($rootScope, $location) {
$rootScope.$on( "$routeChangeStart", function(next) {
$rootScope.username = localStorage.getItem("UserName");
//alert("redirecting to login");
if ($rootScope.username === null) {
// no logged user, redirect to /login
if ( next.templateUrl === "app/components/login/login.html") {
} else {
$location.path("/");
}
}
});
});
答案 0 :(得分:1)
run(function($rootScope, $location) {
$rootScope.$on( "$routeChangeStart", function(next) {
$rootScope.username = localStorage.getItem("UserName");
//alert("redirecting to login");
if ($rootScope.username == null) {
// no logged user, redirect to /login
if ( $location.$$url == "/" || $location.$$url == "/register/") {
// do nothing. Do not redirect
} else {
// redirect to default path
$location.path("/");
}
}
});