无法在 javascript 中使用 promise.push 附加数据

Unable to append the data with promise.push in javascript

本文关键字:push promise 数据 javascript      更新时间:2023-09-26

我想在各自的表格中显示该设置中的所有设置和员工数量。所以我想进行两个不同的http(Get)调用并附加这些结果。所以我的一个朋友建议我去承诺。我尝试了不同的方式,但我失败了。

<html>
<table class="table">
  <tbody>
    <tr ng-repeat="setting in createAttendance.AtSettings">
      <td>
        <div class="col-lg-12">
          <p class="leavesettingpara">
            <strong><span class="parastrong"></span>{{setting.name}}</strong>
          </p>
        </div>
        <div class="col-lg-12">
          <div class="col-lg-3">
            
            <p class="leavesettingpara">{{settings.numOfEmployees}} Employees</p>
          </div>
          <div class="col-lg-3">
            <p class="leavesettingpara">{{setting.date | date:'medium'}}</p>
          </div>
          <div class="col-lg-3">
            <p class="leavesettingpara">By {{setting.createdName}}</p>
          </div>
        </div>
      </td>
    </tr>
  </tbody>
</table>
</html>

控制器:

angular.module('myApp')
  .controller('settingsDisplayController', ['$state', 'attendanceSettingsService'
    function($state, attendanceSettingsService) {
      attendanceSettingsService.getSettings().then(function(data) {
        vm.AtSettings = data;
        console.log("organization" + data);
      });
    }
  ]);

面临的主要问题是"我想要亲子关系。在使用该 result.id 进行第一次http调用成功后,我们希望进行第二次http调用"

服务:

angular.module('myApp')
  .service('attendanceSettingsService', ['$q', '$http', '$state',
    function($q, $http, $state) {
      this.getSettings = function() {
        var d = $q.defer();
        $http.get('api/organizationName').success(function(result) {
          var promises = [];
          var defer = $q.defer();
          var res = [];
          promises.push($http.get('api/organizations/' + result.id + '/attendanceProfileSettings').success(function(res) {
            console.log(res);
          }));
          console.log(res);
          promises.push($http.get('api/setting/' + res.id + '/employees'));
          $q.all(promises).then(function(data, status, headers, config) {
            console.log(data);
            d.resolve(data);
          }, function(data, status, headers, config) {
            console.log(" failure...." + status);
          });
          return defer.promise;
        });
        return d.promise;
      };
    }
  ]);

当您有许多并行承诺时,需要使用此策略,并且希望在成功解析所有承诺时进行回调。

你可能想要什么,它被称为承诺链,它用前一个的答案调用下一个承诺。例:

travelService
            .getDeparture( user )                                           // Request #1
            .then( function( departure )
            {
                $scope.departure = departure;                               // Response Handler #1
                return travelService.getFlight( departure.flightID );       // Request #2
            })
            .then( function( flight )
            {
                $scope.flight = flight;                                     // Response Handler #2
                return weatherService.getForecast( $scope.departure.date ); // Request #3
            })
            .then( function( weather )
            {
                $scope.weather = weather;                                   // Response Handler #3
            });

这是您可能想要查看的承诺链接指南的示例: http://solutionoptimist.com/2013/12/27/javascript-promise-chains-2/