0
我有兩個服務調用,我想按順序同步執行,但即使在使用承諾時也不會發生這種情況。面對AngularJs中的同步調用問題
控制器:
vm.nominationSVC.zipping(vm.fileSelected, vm.selectedCategoryId).
then(function (response: any) { //Zipping
vm.nominationSVC.downloadDocument("documents.zip");
}).
then(function (response: any) {
var deffered = vm.$q.defer();
for (i = 0; i < vm.rowSelectedLength; i++) {
vm.objDownloadHistory.Nomination_Id = vm.nominationIdSelected[i];
vm.objDownloadHistory.FilePath = vm.fileNamesSelected[i];
vm.promises.push(vm.nominationSVC.updateDownloadHistory(vm.objDownloadHistory));
}
// vm.$q.all(vm.promises).then(function() {
// console.log("sdsd");
// });
}).
then(function (response: any) {
vm.getNomiantionList();
});
方法vm.nominationSVC.updateDownloadHistory(vm.objDownloadHistory)不完全執行並歸結爲其他方法。然後即vm.getNomiantionList();
我試過使用$ q.all也是在註釋代碼中提到的,但仍然無法解決此問題。
服務方法:
updateDownloadHistory(objDownloadHistory: SpotAward.DownloadHistory)
{
var vm = this;
var url: any;
var deferred = this.$q.defer();
url = this.BaseUrl + 'DownloadHistory/UpdateDownload';
if (url !== null) {
this.$http.post(
url,
JSON.stringify(objDownloadHistory),
{
headers: {
'Content-Type': 'application/json'
}
}
).then(function (result: any) {
if (result.data > 0)
deferred.resolve(result.data);
}).catch((data) => {
deferred.reject(data);
});
}
return deferred.promise;
}