Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
angularjs从$http.get更改服务属性_Angularjs_Angularjs Scope - Fatal编程技术网

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

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中“保存”响应

注释行(
//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或控制器中?