2014-10-02 155 views
1

如何将其他配置传递给$ 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中获取的数据WITH从'additionalConfig'传递配置?

+0

你真的需要澄清这个问题。我不能告诉你想要做什么或想要在$ q.all.then处理程序中实现什么 – Phil 2014-10-02 06:08:42

+0

我希望不仅能够访问来自ajax响应的数据,还能够访问变量additionalConfig中的数据 - 所以问题是如何通过$ http服务传递这个对象以便能够通过AJAX响应来访问它 - 现在已经足够清楚了? – 2014-10-02 06:10:58

+0

我想这是类似isse -http://stackoverflow.com/questions/939032/jquery-pass-more-parameters-into-callback - 但我想用angularJS $ q服务来解决它。 – 2014-10-02 06:13:20

回答

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); 
    }); 
}); 
+1

作品像一个魅力:) thx – 2014-10-02 06:20:00