简体   繁体   English

Angularjs - 嵌套的 ng-repeat 全局索引

[英]Angularjs - nested ng-repeat global index

I have next template:我有下一个模板:

<div data-ng-repeat="supplier in order.Suppliers" data-ng-init="supplierIndex = $index">
    <div data-ng-repeat="group in supplier.Groups">
         {{something}}
    </div>
</div>

And model:和型号:

$scope.order = {
    Suppliers: [
        {
            Groups: [{ id: 'sss'}, {id: 'ddd'}]
        },
        {
            Groups: [{ id: 'qqqq'}, {id: 'www'}, {id: 'xxx'}]
        },
        {
            Groups: [{ id: 'ooo'}]
        }
    ]
}

I need to display global group index, so output should be like this:我需要显示全局组索引,所以输出应该是这样的:

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

I know that I can use function that calculate index by passed group id at each place we need to display global group index, but how to accomplish this goal most gracefully?我知道我可以在我们需要显示全局组索引的每个地方使用通过传递的组 ID 计算索引的函数,但是如何最优雅地完成这个目标呢?

您可以使用{{$index}}在列表中显示组索引。

You can merge groups like this in your controller.您可以在控制器中合并这样的组。

 $scope.mergedGroups = [];
  for(var i=0;  i < $scope.order.Suppliers.length;  i++){
    for(var k=0;  k < $scope.order.Suppliers[i].Groups.length;  k++){
       $scope.mergedGroups.push($scope.order.Suppliers[i].Groups[k]);
    }
  }

then you can use a single ng-repeat and its done.然后您可以使用单个 ng-repeat 并完成。

<div data-ng-repeat="group in mergedGroups" >
    {{group}} {{$index}}
</div>

Your options:您的选择:

  1. $parent.$index
  2. put supplier index inside supplier object将供应商索引放在供应商对象中
  3. create component <supplier-info supplier="supplier" index="$index">创建组件<supplier-info supplier="supplier" index="$index">

If done only in html:如果仅在 html 中完成:

<div data-ng-init="$parent.index = 0" data-ng-repeat="supplier in order.Suppliers">
  <div data-ng-repeat="group in supplier.Groups">
    <span data-ng-init="index=$parent.$parent.index;$parent.$parent.index = $parent.$parent.index + 1;">
      {{index}}
    </span>
  </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM