AngularJS:如何在ng-repeat中获取当前迭代次数

时间:2016-12-22 18:50:20

标签: angularjs

我有这个ng-repeat循环:

<div ng-repeat="chapter in chapters">
    <div>Chapter #<span>{{ X }}</span>: {{ chapter.name }}</div>
</div>

如何让{{ X }}成为循环中的迭代?

给我一​​个这样的结果:

  

第1章:开始

     

第二章:中间

     

第3章:结束

2 个答案:

答案 0 :(得分:4)

答案 1 :(得分:4)

您可以使用$ index,

var app = angular.module("App", []);
app.controller("Cont", function($scope) {
  var Chapters = [
  {
    "pid": "110",
    "name": "Harry"
   
  },
  {
    "pid": "109",
    "name": "potter"
   
  },
  {
    "pid": "101",
    "name": "Peter"
  
  },
  {
    "pid": "104",
    "name": "Janifer"
    
  }
 
];
  $scope.chapters = Chapters;
});
<!DOCTYPE html>
<html>
<head>
   <script data-require="angular.js@1.4.7" data-semver="1.4.7" src="https://code.angularjs.org/1.4.7/angular.js"></script>

</head>
<body ng-app="App" ng-controller="Cont">
  
        <div ng-repeat="x in chapters track by $index">
          <td> Chapter#{{$index}} {{x.name}}</td>
          
        </div>
      
  </body>

</html>