在元素上使用ng-repeat指令

时间:2016-06-01 09:50:11

标签: angularjs ruby-on-rails-4

我使用Rails作为后端和angularjs用于前端。 在我的angularjs控制器中,我有:

app.controller('ReportsInsurerPaymentsCtrl', ['$scope', '$http', function($scope, $http) {

  $scope.insurerPayments = [];
  $scope.commissions = [];
  $scope.insurers = [];

  $scope.getInsurerPayments = function () {
    $http.get('/reports/insurer_payments.json').success(function (data) {
      $scope.insurerPayments = data.payments;
      $scope.commissions = data.commissions;
      $scope.insurers = data.insurers
    })
  };

  $scope.getInsurerPayments();
}]);

我的Rails控制器为json提供了3个数组:insurerPaymentscommissionsinsurers。在我看来,我想在表格中显示insurerscommissionsinsurerPayments,所以我做了类似的事情:

<table class="table table-bordered table-hover">
  <tr>
    <th>Insurers</th>
    <th>Commissions</th>
    <th>Insurer Payments</th>
  </tr>
  <tr ng-repeat="insurer in insurers">
    <td>{{insurer.name}}</td>
    <td>{{}}</td>
    <td>{{}}</td>
  </tr>
</table>

那么,我怎么能这样做,对不同的数组使用ng-repeat?谢天谢地。

1 个答案:

答案 0 :(得分:1)

使用$ index进行比较

  <tr ng-repeat="insurer in insurers">
    <td>{{insurer.name}}</td>
    <td>{{ commissions[$index] }}</td>
    <td>{{ insurerPayments[$index] }}</td>
  </tr>