循环中$ index的值

时间:2016-05-20 10:15:01

标签: javascript angularjs angularjs-ng-repeat angularjs-ng-model

我有一个带有<input>指令的ng-repeat元素。

  <input ng-repeat="table in tables track by $index" type="text" name="lastName" ng-model="user.$index">

我想在循环中获得$index的值,这是可能的。

3 个答案:

答案 0 :(得分:2)

你显示它是这样的:

{{$index}}

Plunkr

  <ul class="example-animate-container">
    <li class="animate-repeat" ng-repeat="friend in friends | filter:q as results">
      [{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
    </li>
    <li class="animate-repeat" ng-if="results.length == 0">
      <strong>No results found...</strong>
    </li>
  </ul>

答案 1 :(得分:0)

只需在 AngularJS Expresion 中使用$index

EG。

<html ng-app="myApp">
  <head>
    <meta charset="utf-8">
    <title>AngularJS Expression</title>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"></script>
    <script>
      var mod = angular.module('myApp', []);
      mod.controller('myCtrl', function ($scope){
        $scope.tables = ['One', 'Two', 'Three', 'Four', 'Five'];
      });
    </script>
  </head>
  <body ng-controller="myCtrl">
    <input ng-repeat="table in tables track by $index" type="text" name="lastName" value="{{$index}}">
  </body>
</html>

答案 2 :(得分:0)

你可以这样做

<input ng-repeat="table in tables track by $index" type="text" name="lastName" value="{{$index}}">