如何为每2条记录重复表头详细信息

时间:2017-02-03 12:12:48

标签: angularjs angularjs-ng-repeat angular-controller

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 

<table>
  <tr>
    <th></th>
    <th>Name</th>
    <th>Country</th>
  </tr>
  <tr ng-repeat="x in names">
    <td>{{$index+1}}</td>
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("http://www.w3schools.com/angular/customers.php")
    .then(function (response) {$scope.names = response.data.records;});
});
</script>

</body>
</html>

Hai Guys,

我使用ng-repeat显示表记录,$ index + 1显示序列号。

我想显示每2条记录重复的标题名称和国家/地区(如果$ index%2 == 0)。 我该怎么办? 请问。

1 个答案:

答案 0 :(得分:0)

我找到了自己的答案。

 <!DOCTYPE html>
    <html>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <body>

    <div ng-app="myApp" ng-controller="customersCtrl"> 
    <div ng-repeat-start="x in names">
    </div>
    <div ng-if="$index % 2==0">
    <table ng-repeat="x in names | limitTo:1">
       <tr>
        <th>S No</th>
        <th>Name</th>
        <th>Country</th>
      </tr>
     </table>
    </div>
    <table ng-repeat-end="x in names">
      <tr>
        <td>{{$index + 1}}</td>
        <td>{{ x.Name }}</td>
        <td>{{ x.Country }}</td>
      </tr>
    </table>

    </div>

    <script>
    var app = angular.module('myApp', []);
    app.controller('customersCtrl', function($scope, $http) {
        $http.get("customers.php")
        .then(function (response) {$scope.names = response.data.records;});
    });
    </script>

    </body>
    </html>