AngularJs ng-repeat with index

时间:2016-03-04 23:34:43

标签: angularjs angularjs-ng-repeat

我只想在单击显示按钮时在此部分显示名称。我正试图检测使用索引,但我没有成功。

Code

<div ng-repeat="c in customers">

   <a ng-click="display(c.id[$index])" ng-hide="need to hide after click this one">{{vm.updateText}}</a>
   <div ng-show="c.id[$index]" class="updateSection">
        <input type="text" name="id" data-ng-model="id" />
          <input type="button" ng-click="vm.veryId(id)" value="{{vm.verifyButtonText}}">
          <input type="button" ng-click="vm.Cancel()" value="{{vm.cancelButtonText}}">
       </div>
    </div>
    // will show ng-click="vm.veryId(id)"
    <rpe-progress data-ng-show="vm.showProgress" block="true"></rpe-progress>
    // if id is working will show following error message:
    <rpe-alert show="vm.mpnIdAlert">{{vm.errormessage}}</rpe-alert>        



<script>
   vm.display= function (index) {    
      vm.id[index] = true;  
  //   I want show updatesection & hide Update text   
   }
vm.cancel= function () {   
//   I want hide updatesection & show Update text 
       }


  vm.veryId= function (id) {   
    //  If id is wrong show error message.
           }
</script>

4 个答案:

答案 0 :(得分:7)

如果我正确理解您的问题,您应该使用

跟踪

举个例子:

<div ng-repeat="n in [42, 42, 43, 43] track by $index">
  {{n}}
  <p ng-show="$index === 3">Index is 3</p>
</div>

https://docs.angularjs.org/api/ng/directive/ngRepeat

答案 1 :(得分:2)

发布 @Pevara 的逻辑绝对没问题。我建议你顺其自然。

我不了解您问题的实际用例。那么,如果你真的想用$index值来完成这个逻辑,那么你可以使用下面的代码片段来完成你的任务。

&#13;
&#13;
var app = angular.module('app', []);

app.controller('indexCtrl', function ($scope) {
  $scope.customers = [
    {name: 'ABC', Age: 26},
    {name: 'DEF', Age: 32},
    {name: 'GHI', Age: 21}    
  ]; 
  
  $scope.toggleDisplay = function(index) {
    $scope.customers[index].show = ! $scope.customers[index].show;    
  };
  
});
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app">
  <div ng-controller="indexCtrl">
    <table class="table">
      <tr>
        <td>Name</td>
        <td>Age</td>
      </tr>
    <tr ng-repeat="c in customers">
      <td>{{c.name}}</td>
      <td>
        <button ng-click="toggleDisplay($index)">Display</button>            
        <span ng-show="c.show">{{c.Age}}</span></td>
    </tr>
    </table>
  </div>
</body>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您在模板中无法做任何事情,不需要该功能。您需要做的就是在show上切换一些item属性。

看一下这个例子: https://plnkr.co/edit/YS5zhU3pHMxut34XwPtR?p=preview

      <li ng-repeat="item in items">
        <p>{{item.id}}</p>
        <p ng-if="item.show">
          {{item.name}}
        </p>
          <button ng-click="item.show = ! item.show">toggle</button>
      </li>

如果您不想切换,可以执行以下操作:

<button ng-click="item.show = true">show</button>

答案 3 :(得分:1)

如果客户对象数组拥有名称本身,则传递对象本身而不是传递$ index

见这里:https://plnkr.co/edit/I7F4ZT5acm41P1fW58Hr?p=preview

<button ng-click="clickMe(x)">Click Me</button>
$scope.clickMe = function(x){
  $scope.selected = x;
}