我正在研究angularjs代码,在其中创建了一个工厂方法,用于在登录成功后将用户重定向到配置文件页面,并在工厂方法中更新flag变量。我附加在路由上的resolve属性无法正常工作,似乎“ islogged”变量未读取从localstorage检索的值。
.when('/Login',{
templateUrl: "/views/login.html",
controller: "logctrl",
})
.when('/Profile',{
templateUrl: "/views/profile.html",
resolve: ["authService",function(authService){
return authService.checkstatus();
}]
});
});
app.factory("authService",function($location){
return {
"islogged" : localStorage.getItem("islog"),
checkstatus :function() {
if(this.islogged)
return true;
else
$location.path(['/Login']);
return false;
}
}
});
这是登录的控制器:
app.controller('logctrl',function($scope,$http, $location){
localStorage.setItem("islog",false);
$scope.log = function(){
var uname = $scope.username;
var pass = $scope.password;
var logobj = {
"username" : uname,
"password" : pass
};
$http.post("http://localhost:3000/sign",logobj).then((resp)=>{
$location.url(resp.data);
localStorage.setItem("islog",true);
}).catch((err)=>$location.url("/"));
}
});