将$ index从一个ng-repeat传递给另一个ng-repeat

时间:2015-11-21 17:06:06

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

我想迭代列表,例如:

$scope.articles = [
  { id: 1, name: "Pizza Vegetaria", price: 5 },
  { id: 2, name: "Pizza Salami",    price: 5.5 },
  { id: 3, name: "Pizza Thunfisch", price: 6 }
];

如果我第二次使用另一个ng-repeat迭代此列表,我希望$ index继续而不是从头开始。那么是否可以将索引从一个ng-repeat范围传递到另一个? 所以没有得到这个结果:

0   Pizza Vegetaria 5
1   Pizza Salami    5.5
2   Pizza Thunfisch 6
0   Pizza Vegetaria 5
1   Pizza Salami    5.5
2   Pizza Thunfisch 6

我希望得到这样的结果:

0   Pizza Vegetaria 5
1   Pizza Salami    5.5
2   Pizza Thunfisch 6
3   Pizza Vegetaria 5
4   Pizza Salami    5.5
5   Pizza Thunfisch 6

HTML如下所示:

<table class="table">
          <tr ng-repeat="article in articles track by $index">
            <td>{{$index}}</td>
            <td>{{article.name}}</td>
            <td>{{article.price}}</td>
          </tr>
    </table>

    <table class="table">
          <tr ng-repeat="article in articles track by $index">
            <td>{{$index}}</td>
            <td>{{article.name}}</td>
            <td>{{article.price}}</td>
          </tr>
    </table>

提前致谢!

1 个答案:

答案 0 :(得分:1)

没有真正好的方式来传递&#34;索引 - 而只是添加列表的长度以获得下一个开始:

<table class="table">
    <tr ng-repeat="article in articles track by $index">
        <td>{{$index + articles.length}}</td>
        <td>{{article.name}}</td>
        <td>{{article.price}}</td>
    </tr>
</table>