AngularJS - 如何在$ http响应中传递额外的参数

时间:2014-10-02 06:04:33

标签: javascript ajax angularjs angular-promise

如何将其他配置传递给$ http服务响应?代码如下:

var promises = [];
angular.forEach(rows, function(rowIterator) {
    var additionalConfig = {
      config1: rowIterator.config1,
      config2: rowIterator.config2
    }

    promise = $http({
        method: "get",
        url: "http://domain.com/" + rowIterator.videoId + '?appName=playlist',
        params: {}
    });
    promises.push(promise);                   
});

return $q.all(promises).then(function(data) {
    // handle data from all promises WITH passed custom configs
});

那么如何在$ q.all中读取所有已获取的数据并通过来自' additionalConfig' ?

1 个答案:

答案 0 :(得分:2)

这是一个在黑暗中的疯狂刺,但如何链接响应承诺这样......

promises.push(promise.then(function(response) {
    return {
        response: response,
        additionalConfig: additionalConfig;
    };
}));

则...

return $q.all(promises).then(function(data) {
    angular.forEach(data, function(obj) {
        console.log('Response', obj.response);
        console.log('Additional config', obj.additionalConfig);
    });
});