在我的解析器函数中,我有循环,在每个itaration中调用http服务:
function detailedReportDataResolver($stateParams, reportService) {
var promises = [];
var frequenciesArr = $stateParams.frequencies.split(',').map(function (item) { return parseInt(item, 10); });
angular.forEach(frequenciesArr, function (freq, key) {
reportService.getDetailsReport($stateParams.clientId, $stateParams.date, freq).then(function (result) {
promises.push(result.data);
});
});
return $q.all(promises);
}
这里我如何在我的模块中调用解析器函数:
resolve: {
detailedReportData: ["$stateParams", "reportService", detailedReportDataResolver]
}
这是服务:
(function () {
"use strict";
angular.module("reportBuilder").factory("reportService", ["$http", "config", reportService]);
function reportService($http, config) {
var serviceUrl = config.baseUrl + "api/Reports/";
var service = {
getDetailsReport: detailsReport
};
return service;
function detailsReport(clientId, date, frequencies) {
if (!date && !clientId) return null;
return $http.get(serviceUrl + "ReportDetailed/" + clientId + "/" + date + "/" + frequencies);
}
}
})();
我需要在开始下一次循环迭代之前等待http服务完成并返回结果。为此,我在上面的解析器完成中使用$q.all
。
但解析器功能不会返回任何内容。任何想法我做错了什么?为什么旋转变压器功能不会返回任何内容?
答案 0 :(得分:0)
承诺数组未正确组成。你能尝试一下:
function detailedReportDataResolver($stateParams, reportService) {
var promises = [];
var frequenciesArr = $stateParams.frequencies.split(',').map(function (item) { return parseInt(item, 10); });
angular.forEach(frequenciesArr, function (freq, key) {
var promise = reportService.getDetailsReport($stateParams.clientId, $stateParams.date, freq).then(function (result) {
return result.data;
});
promises.push(promise);
});
return $q.all(promises);
}