如何使用 $resource 在 angular 中基于 _.each 迭代等待多个请求

How to wait for multiple requests based on _.each iteration in angular using $resource

您好,我有一个基于 _.each 迭代执行多个请求的方法。我想要做的是在 _.each 迭代

之后在 getAllJobSublinesByJobline 中初始化 vm.job = job
    var vm = this;

    function getAllJobSublinesByJobline () {
        return $resource( '/api/joblines/get_all_jobsublines_by_jobline/:pageLimit', {
            'jobLineId' : '@jobLineId',
            'page'      : '@page',
            'search'    : '@search',
            'sortField' : '@sortField',
            'sortType'  : '@sortType'
        } );
    }

    function getJobsublinesByJoblineId ( joblines, job ) {
        _.each( joblines, function ( jobline ) {
            if ( parseInt( jobline.num_sublines ) ) {
                var jobsublineFetchDetails = {
                    'pageLimit' : 10,
                    'jobLineId' : jobline.id
                };
                return getAllJobSublinesByJobline().save( jobsublineFetchDetails ).$promise.then( function ( jobsubline ) {
                    jobline.jobsublines = jobsubline.data;
                    job.joblines.push( jobline );
                } );
            }
            job.joblines.push( jobline );
        } );
        vm.job = job; // initializes right away even though _.each iteration is not finished yet
    }

我的问题是它立即初始化,即使 _.each 迭代还没有完成获取数据。为什么会这样?

您可能可以将所有承诺推送到一个数组并使用类似

的东西
$q.all(yourPromises).then(function(allCompletedResponse){...}).

来自文档:

It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray). Once the data is returned from the server the existing reference is populated with the actual data.

-- AngularJS $resource API Reference

正如另一个答案所说,推送承诺,并使用 $q.all

function getJobsublinesByJoblineId ( joblines, job ) {
    var promises = [];
    _.each( joblines, function ( jobline ) {
        if ( parseInt( jobline.num_sublines ) ) {
            var jobParams = {
                'pageLimit' : 10,
                'jobLineId' : jobline.id
            };
            var details = getAllJobSublinesByJobline().save( jobParams );
            promises.push(details.$promise);
        }
    } );
    $q.all(promises).then (function (detailsArray) {
         //assemble job object
         vm.job = job;
    });
}

因为我需要在拥有工作子线时初始化工作线,所以我对我所做的与上面的答案类似。

    function getJobsublinesByJoblineId ( joblines, job ) {
        var promises = _.map( joblines, function ( jobline ) {
            var jobParams = {
                'jobLineId' : jobline.id
            };

            var deferred = $q.defer();

            apiService.getAllJobSublinesByJobline().save( jobParams ).$promise.then( function ( jobsubline ) {
                jobline.jobsublines = jobsubline.data;
                deferred.resolve( jobline );
            } );

            return deferred.promise;
        } );

        $q.all( promises ).then( function ( joblines ) {
            job.joblines = joblines
            vm.job       = job;
        } );
    }