0
我想做一個http請求,並將返回的結果放入我的數組中。如何在我的示例中獲取多個http請求結果?
我有類似
按Ctrl
for (var i=0; i < numOfProduct.length; i++) {
productFactory.makeRequest(numOfProduct[i].id)
.then(function(data) {
console.log(i)
//always output 10 which is my numOfProduct.length
numOfProduct[i].push({detail: data});
//i got Cannot read property 'push' of undefined error
})
$scope.numOfProduct = numOfProduct;
}
productFactory
service.makeRequest = function(id) {
return $http.get('/api/product/get' + id);
}
return service;
我的目標是一個http請求的結果推到每個numOfProduct元素作爲對象。但是,我似乎無法使用http請求代碼來做到這一點。任何人都可以幫助我嗎?非常感謝!
我不知道,但我想這是由於異步的ajax調用。然後在函數中分配$ scope.numOfProduct = numOfProduct; – 2014-11-02 23:24:19