是否可以通过从某个索引开始循环并在另一个索引处结束循环来“ng-repeat”来限制显示结果的数量

时间:2015-03-12 20:10:58

标签: angularjs angularjs-directive

我想实现此代码

a[10];

for(int i=3;i<7;i++)

    a[i]

是否可以使用ng-repeat

实现这种类型的循环

3 个答案:

答案 0 :(得分:2)

你可以(即使它是丑陋的imho):

<div ng-repeat="int in ints | limitTo: -7 | limitTo: 4">
    {{ int }} at {{ $index }}
</div>

example with numbers

example with 'stuffs'

答案 1 :(得分:1)

Angular过滤器很花哨,但是对于这个特定的用例你不需要它们,因为普通的javascript Array.prototype.slice完全符合你的要求:

<div ng-controller="MyCtrl">
    <!-- $scope.ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] -->
    <div ng-repeat="int in ints.slice(3, 7)">
        {{ int }} at {{ $index }}
    </div>
</div>

结果:

3 at 0
4 at 1
5 at 2
6 at 3

See it in action.

答案 2 :(得分:0)

limitTo采用可选的第二个参数......

https://docs.angularjs.org/api/ng/filter/limitTo