Angular chaining promises from foreach loop

后端 未结 2 550
天涯浪人
天涯浪人 2021-01-23 18:34

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 ()          


        
2条回答
  •  南笙
    南笙 (楼主)
    2021-01-23 18:37

    You can do that with $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);
        })
    };
    

提交回复
热议问题