如何在ng-repeat中制作独特的ng-model

时间:2017-09-23 11:00:43

标签: angularjs variables angularjs-scope angularjs-ng-repeat

我想在下面的代码中将每个test(ng-model)设为test1test2唯一。

<div ng-repeat="item in Array">
  <div>{{item.Name}}</div>
  <a ng-click="openClose(test)>show/hide</a>
  <div ng-show="test">{{item.Des}}</div>
</div>

$scope.openClose = function (modalName) {
  $scope[modalName] = $scope[modalName] ? false : true;
}

1 个答案:

答案 0 :(得分:1)

使用show / hide时,您可以使用每个元素获得的$index值来维护ng-repeat

&#13;
&#13;
angular.module('app',[]).controller('mainCtrl', function($scope){
   
    $scope.Array = [{Name:'abc'},{Name:'zzz'},{Name:'yyy'},{Name:'xxx'}];
    
    $scope.openClose = function (index) {
      if($scope.selectedValue == index){
        $scope.selectedValue = -1;
      }else{
      	 $scope.selectedValue = index;
      }
    }
    
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='app' ng-controller='mainCtrl'>
   <div ng-repeat="item in Array">
     <div>{{item.Name}}</div>
      <a ng-click="openClose($index)">show/hide</a>
     <div ng-show='$index === selectedValue'>Hide Show content</div>
  </div>
</div>
&#13;
&#13;
&#13;