具有一个索引的嵌套ng-repeat

时间:2018-10-07 15:42:04

标签: angularjs angularjs-ng-repeat

我有此代码,并且我想为第二个ng-repeat中的所有项目提供一个索引 现在索引看起来像这样:

0 0
0 1
0 2
1 0
1 1
1 2

我希望他们看起来像这样:

0 0
0 1
0 2
1 3
1 4
1 5

如果我搜索“ qwe”,我希望它看起来像这样

0 0
0 1 
0 2

如何实现(请注意ng-如果它过滤了文本)?

var app = angular.module('github', []);
app.controller('githubRepos', ['$scope','$http', function($scope, $http){  
  $scope.text = '';
   $scope.groups = {
     1: ['asd', 'asd1', 'asd2'],
     2: ['qwe', 'qwe1', 'qwe2']
   }
  
  $scope.show = function(text) {
    return !$scope.text || text.indexOf($scope.text) !== -1;
  }
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="github">
  <div ng-controller="githubRepos">
    <input type="text" ng-model="text" />
    <div ng-repeat="group in groups track by $index" ng-init="parentIndex = $index">
      <div ng-repeat="item in group track by $index" ng-if="show(item)">
        {{parentIndex}} {{$index}} {{item}}
      </div>
    </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:4)

您可以使用此:

  

{{parentIndex}} {{$ index +(group.length * parentIndex)}} {{item}}

答案 1 :(得分:2)

    <div ng-app="github">
  <div ng-controller="githubRepos">
    <input type="text" ng-model="text" />
    <div ng-repeat="group in groups track by $index" ng-init="parentIndex = $index">
      <div ng-repeat="item in group track by $index" ng-if="show(item)">
        {{parentIndex}} {{$index + group.length * parentIndex}} {{item}}
      </div>
    </div>
  </div>
</div>

如果您有任何问题,请对其进行评论