angularjs从$ http.get更改服务属性

时间:2014-02-06 16:07:59

标签: angularjs angularjs-scope

以下代码应检查用户是否已登录并在accountService中“保存”响应。

注释行(//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');
            }
        });
    };
}

0 个答案:

没有答案