【问题标题】:passing $index from one ng-repeat to another将 $index 从一个 ng-repeat 传递到另一个
【发布时间】:2015-11-21 17:06:05
【问题描述】:

我想遍历一个列表,例如:

$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>

提前致谢!

【问题讨论】:

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


    【解决方案1】:

    没有真正的“传递”索引的好方法 - 只需添加列表的长度即可获得下一个开始:

    <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>
    

    【讨论】:

    • 好吧,同样的想法呵呵。将删除我的答案,因为它与您的相同
    猜你喜欢
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 2013-02-21
    相关资源
    最近更新 更多