从foreach循环链角承诺 [英] Angular chaining promises from foreach loop
本文介绍了从foreach循环链角承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的照片文件的数组,需要上传到Azure的云存储和使用foreach循环如下调用上传我:
I have an array of photo files that needed to upload to Azure Cloud Storage, and i using foreach loop to call upload as below:
$scope.savetemplate = function () {
var imagePathsArray = [];
$scope.filesimage = [];
$scope.filesimage.push($scope.file1);
$scope.filesimage.push($scope.file2);
$scope.filesimage.push($scope.file3);
for (var i in $scope.filesimage) {
$scope.upload($scope.filesimage[i]);
}
$scope.data.Images = imagePathsArray ;
$http({
//after finish uploads i need to post the paths
//of all images to save into database
})
};
$scope.upload = function (file) {
Upload.upload({
url: '/uploadImage',
data: { file: file }
}).then(function (resp) {
imagePathsArray.push(resp.data);
})
};
resp.data STRONG>返回Azure存储路径和我需要的路径推入imagePathsArray
resp.data returns azure storage path and i need to push the paths into the imagePathsArray
我如何使用角度无极等待上传完成了所有的档案都存储在imagePathsArray的路径,所以我可以用
How can i uses Angular Promise to wait for upload all the files finished and all the paths are stored in the imagePathsArray so i can proceed with
$scope.data.Images = imagePathsArray ;
,这样我可以得到数组中的路径和执行$ HTTP POST?
so that i can get the paths in the array and perform $http post?
推荐答案
您可以做到这一点的 $ q.all 。
var promises = [];
for (var i in $scope.filesimage) {
promises.push(upload($scope.filesimage[i]));
}
$q.all(promises).then(function() {
$scope.data.Images = imagePathsArray ;
$http.post({
//after finish uploads i need to post the paths
//of all images to save into database
});
});
function upload(file) {
return Upload.upload({
url: '/uploadImage',
data: { file: file }
}).then(function (resp) {
imagePathsArray.push(resp.data);
})
};
这篇关于从foreach循环链角承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文