【问题标题】:value of $index in the loop循环中 $index 的值
【发布时间】:2016-05-20 18:41:19
【问题描述】:

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

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

我想要循环中$index的值,这是可能的。

【问题讨论】:

  • 需要展示还是制作动态html元素?
  • ng-model="user[$index]" 是这个吗?顺便说一句,什么是用户?你能发布那个数组吗?
  • "user[$index]" 不起作用
  • 你可以使用{{$index}}

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


【解决方案1】:

你会这样展示它:

{{$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>

【讨论】:

    【解决方案2】:

    只需在 AngularJS Expresion 中使用 $index

    例如。

    <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>
    

    【讨论】:

    • &lt;div ng-app="testApp" ng-controller="testController"&gt; &lt;form&gt; &lt;div ng-repeat="table in tables track by $index" &gt; &lt;input ="text" name="name"&gt;&lt;br&gt; &lt;/div&gt; &lt;/form&gt; &lt;button type="button" ng-click="addtable()"&gt; + &lt;/button&gt; {{tables}} &lt;/div&gt; &lt;script type="text/javascript"&gt; var app = angular.module('testApp', []); app.controller('testController', function($scope) { $scope.tables=[""]; $scope.addtable = function() { $scope.tables.push(" "); }; }); &lt;/script&gt;
    【解决方案3】:

    你可以这样做

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

    【讨论】:

    • 应该是$index 而不是index
    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 2019-01-10
    相关资源
    最近更新 更多