ng-repeat 指定起始索引 [英] ng-repeat specify a starting index
问题描述
如何指定 ng-repeat 指令将要开始的索引而不是零?
How can I specify an index where an ng-repeat directive is going to start instead of zero?
我有一组结果,我只需要指定与零不同的起始结果.
I have a set of results and I just need to specify the starting one, which is different from zero.
推荐答案
无需编写任何代码,Angular 已经通过 现有 内置的 limitTo 过滤器.只需使用负限制.来自 limit
参数的文档:
No need to code anything, angular has done this with the existing built-in limitTo filter. Just use a negative limit. From the docs for the limit
argument:
返回的数组或字符串的长度.如果限制数是正数,限制从源头开始的项目数数组/字符串被复制.如果数量为负数,限制数量复制源数组/字符串末尾的项目.限制如果超过 array.length 将被修剪.如果未定义限制,则输入将原样返回.
The length of the returned array or string. If the limit number is positive, limit number of items from the beginning of the source array/string are copied. If the number is negative, limit number of items from the end of the source array/string are copied. The limit will be trimmed if it exceeds array.length. If limit is undefined, the input will be returned unchanged.
所以你会像这样在模板中使用它:
So you would use it like so in the template:
<ul>
<li data-ng-repeat="i in list | limitTo: (offset - list.length)">{{i}}</li>
</ul>
其中 offset
是您的起始索引.请参阅示例 plunker.
where offset
is your start index. See sample plunker.
有一个可选的 begin
参数,因此您不需要使用负 limit
但 begin
在 angular v1 之前不可用.4 所以我在我的例子中坚持了一个负 limit
.
There is an optional begin
argument so you don't need use a negative limit
but the begin
was not available before angular v1.4 so I have stuck to a negative limit
in my example.
这篇关于ng-repeat 指定起始索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!