Angularjs can';t检查用户配置文件服务cll中的
我试图检查用户配置文件,然后授权或不授权,以便访问某个页面。 我正在定义这个服务Angularjs can';t检查用户配置文件服务cll中的,angularjs,angular-ui-router,Angularjs,Angular Ui Router,我试图检查用户配置文件,然后授权或不授权,以便访问某个页面。 我正在定义这个服务 .factory( 'accountService', function($resource, $http, sessionService, $state) { var service = {}; service.getProfile = function() {
.factory(
'accountService',
function($resource, $http, sessionService, $state) {
var service = {};
service.getProfile = function() {
$http.get("/myapp/rest/account/getuserprofile").success(function(data) {
userProfile = data.userProfile;
if (userProfile == "1"){
service.isStudent = false;
console.log("1", service.isStudent);
}
else
service.isStudent = false;
console.log("2", service.isStudent);
}).error(function() {
});
return service.isStudent;
}
return service;
})
那么我要检查这项服务
$scope.$on('$stateChangeStart', function(event, toState,
toParams, fromState, fromParams) {
$scope.uiRouterState = $state;
if ((toState.data.authenticate
&&accountService.getUser())) {
$state.go("login");
event.preventDefault();
}
if ((toState.data.teacher &&accountService.isStudent ===1)) {
$state.go("home");
event.preventDefault();
}
});
但问题是accountService.isStudent始终是未定义的。
如何检查?是否只检查
accountService.isStudent
是否未定义?或者问题是,accountService.isStudent
始终未定义,因此您不知道该帐户是否为学生?@colelemonz我需要检查该帐户是否为学生,如果是学生,则不允许他访问该页面,否则将允许他访问该页面。@Med您异步获取数据,但同步返回service.isStudent。它总是未定义的