【发布时间】:2016-03-15 23:02:46
【问题描述】:
我有这个代码:
// HTTP request
$http.get(dataSource).
then(function(data, status, headers, config) {
// Products array
var products = [];
// Loop through each array value
for (var slug in data.data){
var product = data.data[slug];
$http.get('content/products/' + product + '.json').then(function(response){
products.push(response.data);
$scope.products = products;
}).catch(function(){
console.log('there was an error');
});
}
}).catch(function(){
console.log('there was an error');
});
问题在于,有时产品范围数组项的到达顺序并不总是与请求的顺序相同。
我需要产品 $scope 来循环遍历数组,并且仅当响应被推送到数组时:
products.push(response.data);
最终分配给变量$scope.products。
对修改我当前的 HTTP 请求有帮助吗?
【问题讨论】:
标签: javascript angularjs xmlhttprequest