我有一个页面,它会调用api来返回一页的记录。这是很多信息,需要时间来加载。我试图链接承诺,以便在显示它的页面加载之前加载数据。我使用以下内容来调用第一部分:
$scope.selectAll = function () {
alert('Please wait while your data is loaded...');
$scope.print.sections = angular.copy($scope.sections);
var promise = getAllSections($scope.print.sections);
promise.then(function () {
$scope.dataLoading = false;
alert('Dataload complete!');
})
};
function getAllSections(sections) {
var deferred = $q.defer();
deferred.notify('Getting Data');
for (var i = 0; i < $scope.print.sections.length; i++) {
getSectionData($scope.print.sections[i]);
}
deferred.resolve('Data Complete')
return deferred.promise;
};
我的问题是函数“getSectionData”在我的服务中调用了几个函数(为简洁起见,这只是服务的一部分):
angular.module('myModule').service('contractorService', [
'$http', '$q', function ($http, $q) {
this.getcompletioncertificatepage = function () {
return $http.post('/SSQV4/SSQV5/Contractor/GetCompletionCertificate')
};
this.getselectedcontractorservices = function (id) {
return $http.post('/SSQV4/SSQV5/Contractor/GetSelectedContractorServices?sectionId=' + id)
}
this.getavailablecontractorservices = function (id) {
return $http.post('/SSQV4/SSQV5/Contractor/GetAvailableContractorServices?sectionId=' + id)
};
this.getgeneraluploadimage = function (id) {
return $http.post('/SSQV4/SSQV5/Contractor/GetGeneralUploadDocument?imageType=' + id)
};
this.getemrtabulation = function () {
return $http.post('/SSQV4/SSQV5/Contractor/GetEMRTabulationColumns')
};
this.getimage = function (id, docDate) {
return $http.post('/SSQV4/SSQV5/Contractor/GetImage', {"emrDetailID": id, "docDate" : docDate})
};
this.getemrquestion = function () {
return $http.post('/SSQV4/SSQV5/Contractor/GetEMRQuestion')
};
在我的控制器中,我称之为:
function getSectionData(section) {
var id = section.QuestionSectionID;
contractorService.getIncidentColumns(clusterId)
.success(function (data) {
...}
contractorService.getgeneralincidentquestions(id)
.success(function (data) {
...}
... (there are many calls to the service here)
}
我正在试图弄清楚如何使用服务的promises为我必须调用的每个函数并链接它们,以便我的“selectAll”函数中的“then”函数不会显示直到所有get函数已经完成。我试着把它放在servcie中:
this.getIncidentDetailByQtr = function (id) {
var deferred = $q.defer();
$http.post('/SSQV4/SSQV5/Contractor/GetIncidentDetailByQtr?tabId=' + id)
return deferred.promise;
};
这在控制器中:
var promise = contractorService.getIncidentDetailByQtr(7)
.then(function (data) {
$scope.qtrDetail = data;
deleteTableRows($scope.qtrDetail);
});
有效,直到我尝试向其他调用添加更多承诺(即var promiseA等)。
当我尝试这样做时:
var promise = contractorService.getIncidentDetailByQtr(7)
.then(function (data) {
$scope.qtrDetail = data;
deleteTableRows($scope.qtrDetail);
});
$scope.incidentTableDefinition = '<table>' + firstRow + secondRow + thirdRow + '<tr style="text-align:center" ng-repeat="q in qtrDetail">' + repeatRow + '</tr></table>';
firstRow = '';
secondRow = '';
thirdRow = '';
repeatRow = '';
var promiseA = contractorService.getincidentsummarycolumns(9)
.then(function (data) {
$scope.summaryColumns = data;
我收到错误“无法获取属性”,然后是“未定义或空引用”。
函数“getSelectedData”对服务进行大约15次调用。我需要初始“解决”,直到所有这些都完成后才执行。我是新手,但我很确定这与链接承诺有关,但我无法弄清楚如何在这里完成我需要的东西。非常感谢任何帮助!
答案 0 :(得分:0)
在对&#34; $ q.all&#34;:
进行一些研究后,有以下工作 $scope.selectAll = function () {
alert('Please wait while your data is loaded...');
$scope.print.sections = angular.copy($scope.sections);
var promise = getAllSections($scope.print.sections);
promise.then(function () {
$scope.dataLoading = false;
alert('Dataload complete!');
})
};
function getAllSections(sections) {
var promises = [];
angular.forEach(sections, function (section) {
var promise = getSectionData(section)
promises.push(promise);
});
return $q.all(promises);
}
希望这有助于其他人。快乐的编码!