0
我正在使用forEach循環上傳圖像(API調用),我希望循環等待直到前一個索引的響應到達。 這裏是我的代碼:在foreach循環中一次執行一個API調用
var uploadCount = 0;
angular.forEach($scope.selectedLayouts, function (layout, pIndex) {
angular.forEach(layout.data, function (data, cIndex) {
if(data.imageIsChanged && $scope.uploadImgCount !== 0) {
Upload.upload({
url: window.apiUrl + 'image_upload',
data: {
'image' : data[$scope.indexes.image]
}
}).then(function(resp) {
uploadCount++;
data[$scope.indexes.image] = resp.data.path;
if($scope.uploadImgCount === uploadCount) {
$scope.uploadImgCount = uploadCount = 0;
}
}, function(error) {
uploadCount++;
toastr.error(error);
});
}
});
});
Upload.upload(我正在使用ng-file-upload模塊)用於API調用,所以我想打破這個循環直到前一個調用的響應。我嘗試了上面的代碼,但仍然遇到了同樣的問題。 –
好的,看我更新的答案 – sjokkogutten