我想知道我们如何使用 AngularJS 进行顺序 Web 服务调用?为简化起见,当我们得到第一个 web 服务的结果时,我们调用第二个。我已经找到了解决方案,但我不太喜欢它。我认为必须有更好的解决方案,这就是我发布问题的原因。这是我的代码(它有效)。
var uploadFile = function(file) {
szUrl = globalServ.datas.szUrl+"transactions/"+globalServ.transactionId+"/file";
var postData = {};
postData['name'] = "MyFile"+file;
postData['file'] = $scope.wsFiles[file];
$http({
method: 'POST',
url:szUrl,
headers: {'Content-Type': false, 'Authorization': globalServ.make_base_auth()},
transformRequest: formDataObject,
data:postData,
}).success(function(response) {
$scope.qFiles[file].resolve(file);//This will allow to call "then" method
}).error(function (data, status, headers, config) {
$scope.errorMsg = globalServ.getErrorMsg(status, data);
$scope.filesAdded = false;
});
}
$scope.uploadFiles = function() {
delete $scope.errorStatus;
$scope.qFiles = [];
$scope.uploadOver = false;
for(file in $scope.wsFiles) {
var q1 = $q.defer();
q1.promise.then(function(curFile) {
if(curFile < $scope.wsFiles.length - 1) {
uploadFile(curFile+1);
} else {
$scope.uploadOver = true;//We uploaded all contracts so now can stop to show loading button
}
});
$scope.qFiles.push(q1);
}
uploadFile(0);//We start to upload contracts
};
欢迎所有批评。先感谢您。