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 你能在角度工厂中使用promise.then()吗?_Angularjs_Promise - Fatal编程技术网

Angularjs 你能在角度工厂中使用promise.then()吗?

Angularjs 你能在角度工厂中使用promise.then()吗?,angularjs,promise,Angularjs,Promise,我试图使用一个承诺将对象返回到下面函数中定义的数据变量。如果我在控制器中使用promise并使res=to$scope.data,则效果很好,但我越想,就越没有必要在$scope中包含此信息,因为用户登录信息/会话ID不应该更改 (function(){ 'use strict'; angular.module('csMgmtApp') .factory('agentFactory', ['$http', function($http){ //other varia

我试图使用一个承诺将对象返回到下面函数中定义的数据变量。如果我在控制器中使用promise并使res=to$scope.data,则效果很好,但我越想,就越没有必要在$scope中包含此信息,因为用户登录信息/会话ID不应该更改

(function(){
    'use strict';

angular.module('csMgmtApp')
    .factory('agentFactory', ['$http', function($http){

     //other variables set successfully by another method

        var data = {},
            result = {},
            access_token = result.access_token,
            baseURIc = result.resource_server_base_uri;


       function startSession(startSessionPayload){
            var startSessionPayload = startSessionPayload,
                access_token = result.access_token,
                baseURIc = result.resource_server_base_uri;

            return $http({
                'url': baseURIc + 'services/v6.0/agent-sessions',
                'method': 'POST',
                'headers': {'Authorization': 'bearer ' + access_token, 'content-Type': 'application/json'},
                'data': startSessionPayload
            }).then(function(res){
                res = data;
                console.log("sessionId", res);
            });
        }
          return {startSession:startSession};
}]);

})();

问题是,当在工厂中尝试此操作时,res作为一个空对象输出。我所尝试的是可能的/明智的吗

您正在将数据定义为空对象

 //other variables set successfully by another method

    var data = {}, // <-- here
            res = data; // <-- data is defined as empty object above
            console.log("sessionId", res); 
难怪它会记录一个空对象