angularjs从$http.get更改服务属性
以下代码应检查用户是否已登录,并在accountService中“保存”响应 注释行(angularjs从$http.get更改服务属性,angularjs,angularjs-scope,Angularjs,Angularjs Scope,以下代码应检查用户是否已登录,并在accountService中“保存”响应 注释行(//accountService.currentUser={username:'test'};)起作用,但只要我在$http回调中更改服务属性,它就不起作用。那么我错过了什么 app.config(function($routeProvider) { $routeProvider .when('/', { templateUrl: 'partials/portal/index.h
//accountService.currentUser={username:'test'};
)起作用,但只要我在$http回调中更改服务属性,它就不起作用。那么我错过了什么
app.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'partials/portal/index.html',
resolve: {
loggedin: isLoggedIn
}
})
var isLoggedIn = function($q, $timeout, $http, $location, accountService){
var deferred = $q.defer();
//accountService.currentUser = {username : 'test'};
$http.get('/api/account/isLoggedIn').success(function(user){
if (user) {
$timeout(function () {
accountService.currentUser = user;
deferred.resolve();
}, 0);
} else {
accountService.currentUser = null;
$timeout(deferred.reject, 0);
$location.path('/login');
}
});
};
}
编写时,
user
的输出是什么:accountService.currentUser=user代码>为什么不将整个isLoggedIn函数移到accountService中,然后将accountService注入app.config->resolve:{loggedin:accountService.isLoggedIn}@doodeec我以为我只能向app.config注入提供者,而不能向app.config注入服务?@MaximShoustin使用accountService的指令没有更新,只是认为用户仍然在使用null@kannix我不确定这是否可能,这只是一个想法。。。不管怎样,为什么你要在配置中做这些,而不是在appRun或控制器中?