ฟังก์ชันตัวแก้ไขจะไม่ส่งคืนสิ่งใด

ในฟังก์ชันตัวแก้ไขของฉันฉันมีลูปที่เรียกใช้บริการ 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 ในฟังก์ชันรีโซลเวอร์ด้านบน

แต่ฟังก์ชันตัวแก้ไขไม่ส่งคืนสิ่งใดๆ ทราบไหมว่าฉันทำอะไรผิด ทำไมฟังก์ชันตัวแก้ไขจึงไม่ส่งคืนสิ่งใด


person Michael    schedule 24.07.2016    source แหล่งที่มา


คำตอบ (1)


อาร์เรย์ของคำสัญญาไม่ได้ถูกจัดวางอย่างถูกต้อง คุณลองได้ไหม:

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);         
}
person Arun Ghosh    schedule 24.07.2016
comment
ขอบคุณ ฉันลองวิธีของคุณแล้ว แต่ฟังก์ชั่นยังคงไม่ส่งคืนข้อมูลที่รวบรวมโดย http servicve - person Michael; 24.07.2016