简体   繁体   English

处理从Angular $ q.all返回的结果

[英]Handling Results Returned from Angular $q.all

I'm somewhat confused on how the results return from $q.all should be handled. 我对如何处理从$ q.all返回的结果感到有些困惑。

Here is my code... 这是我的代码...

  var pageLoad = function () {

        var pageLoadPromises = [
            youEmployerData.getEmployerPrograms($scope.employerId),
            youEmployerData.getWorksites($scope.employerId) 
        ];

        $q.all(pageLoadPromises)
        .then(function (results) {

            $scope.programs = results[0];
            $scope.worksites = results[1];
            $scope.appLoaded = true;

        }, function (r) {

            handleResourceError(r);

        });

    };

Here are my actual resource calls.. 这是我实际的资源电话。

you.factory('youEmployerData', function ($resource) {

    return {

        getEmployerPrograms: function(employerId) {
             return $resource("/api/programs/getemployerprograms?employerId=:id").query({ id: employerId });
         },

        getWorksites: function (employerId) {
             return $resource("/api/employer/getworksites?employerId=:id").query({ id: employerId });
         }
      }
   }

I have verified my resource calls work correctly, I just know I'm doing something wrong inside the then(function(results) { - I'm just not quite sure how to handle that data. 我已经验证了我的资源调用是否可以正常工作,我只是知道我在then(function(results) { -我只是不太确定如何处理该数据。

Also - $scope.worksites should be an array of worksites that is returned, and $scope.programs an array of programs. 另外- $scope.worksites应该是返回的$scope.programs数组,而$scope.programs是一系列程序。

Please let me know if you have any questions or need additional clarification. 如果您有任何疑问或需要其他说明,请告诉我。

Here is the screenshot of console.log(results) - which appears correct, results[0] only have 1 item and results 1 ($scope.worksites) - having multiple items, the only problem is something isn't working because the $scope.worksites it not outputting on the screen correctly, its like the array isn't correct or something. 这是console.log(results)的屏幕截图-看上去正确,结果[0]仅包含1个项目,结果1 ($ scope.worksites)-具有多个项目,唯一的问题是某些东西无法正常工作,因为$ scope.works认为它无法在屏幕上正确输出,就像数组不正确一样。 Sorry - hard to describe. 对不起-难以描述。

在此处输入图片说明

Maybe it is because you missed the fact that a $resource object is not a promise? 也许是因为您错过了$ resource对象不是诺言的事实吗? Can you try with: 您可以尝试:

    var pageLoadPromises = [
        youEmployerData.getEmployerPrograms($scope.employerId).$promise,
        youEmployerData.getWorksites($scope.employerId).$promise 
    ];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM