How can I specify an index where an ng-repeat directive is going to start instead of zero?
如何指定一个索引,让一个ng-repeat指令从0开始?
I have a set of results and I just need to specify the starting one, which is different from zero.
我有一组结果,我只需要指定起始值,它与0不同。
8 个解决方案
#1
49
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:
不需要对任何东西进行编码,angle已经用现有的内置limitTo过滤器完成了这一点。只用一个负的极限。来自文档的极限论点:
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.
返回数组或字符串的长度。如果限制数为正数,则复制源数组/字符串开头的项的限制数。如果数字为负数,则复制源数组/字符串末尾的项目数量。如果超过了array.length,限制将被修改。如果限制未定义,则返回的输入将保持不变。
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>
where offset
is your start index. See sample plunker.
偏移量是起始索引。看到示例砰砰作响。
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.
这里有一个可选的begin参数,所以你不需要使用负极限,但是begin在角度v1.4之前是不可用的,所以在我的例子中我坚持了一个负极限。
#2
36
You can create a filter
您可以创建一个过滤器
app.filter('startFrom', function() {
return function(input, start) {
if(input) {
start = +start; //parse to int
return input.slice(start);
}
return [];
}
});
and then you just use it on the ng-repeat:
然后在ng-repeat上使用
<div ng-repeat="item in items | startFrom : 2">{{item.Name}}</div>
#3
35
The answers seems to be quite old,
答案似乎很古老,
since angular 1.4.0
the limitTo filter takes two parameters
由于角1.4.0,限制滤波器有两个参数
limitTo: (limit) : (begin)
limitTo:(限制)(开始):
you can say ng-repeat="item in list | limitTo:50:0"
可以写成ng-repeat=" list | limitTo:50:0"
this seems to be a much effective solution rather using two different filters.
这似乎是一个非常有效的解决方案,而不是使用两个不同的过滤器。
https://code.angularjs.org/1.4.0/docs/api/ng/filter/limitTo
https://code.angularjs.org/1.4.0/docs/api/ng/filter/limitTo
#4
31
Say you have a list of items
, you can do it like this:
如果你有一个项目列表,你可以这样做:
<div ng-repeat="item in items" ng-if="$index >= myIndex">
{{item.Name}}
</div>
#5
4
I would create a function that returns the desire subarray. This can go in the controller or the link function of a directive.
我将创建一个返回desire子数组的函数。这可以进入控制器或一个指令的链接函数。
<span ng-repeat="item in getSubArray(0, 4)">...</span>
And the function
和功能
$scope.getSubArray = function (start, end) {
return array.slice(start, end);
}
#6
2
This worked for me:
这工作对我来说:
<element ng-repeat="round in view.rounds track by $index">
<span>{{$index + 1}}</span>
</element>
#7
2
This could do the trick...
这可以达到目的……
<div ng-repeat="(i, item) in items">
<p>{{i+1}}</p>
</div>
#8
0
Here is the result of what I was looking for:
这是我寻找的结果:
angular.module('starter.filters', []).filter('startFrom', function() {
return function(input, start) {
if(input) {
start = +start; //parse to int
appended = input.slice(0,start);
initialArray = input.slice(start);
finalArray= initialArray.concat(appended);
return finalArray;
}
return [];
}
});
thanks @emanuel
由于@emanuel
#1
49
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:
不需要对任何东西进行编码,angle已经用现有的内置limitTo过滤器完成了这一点。只用一个负的极限。来自文档的极限论点:
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.
返回数组或字符串的长度。如果限制数为正数,则复制源数组/字符串开头的项的限制数。如果数字为负数,则复制源数组/字符串末尾的项目数量。如果超过了array.length,限制将被修改。如果限制未定义,则返回的输入将保持不变。
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>
where offset
is your start index. See sample plunker.
偏移量是起始索引。看到示例砰砰作响。
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.
这里有一个可选的begin参数,所以你不需要使用负极限,但是begin在角度v1.4之前是不可用的,所以在我的例子中我坚持了一个负极限。
#2
36
You can create a filter
您可以创建一个过滤器
app.filter('startFrom', function() {
return function(input, start) {
if(input) {
start = +start; //parse to int
return input.slice(start);
}
return [];
}
});
and then you just use it on the ng-repeat:
然后在ng-repeat上使用
<div ng-repeat="item in items | startFrom : 2">{{item.Name}}</div>
#3
35
The answers seems to be quite old,
答案似乎很古老,
since angular 1.4.0
the limitTo filter takes two parameters
由于角1.4.0,限制滤波器有两个参数
limitTo: (limit) : (begin)
limitTo:(限制)(开始):
you can say ng-repeat="item in list | limitTo:50:0"
可以写成ng-repeat=" list | limitTo:50:0"
this seems to be a much effective solution rather using two different filters.
这似乎是一个非常有效的解决方案,而不是使用两个不同的过滤器。
https://code.angularjs.org/1.4.0/docs/api/ng/filter/limitTo
https://code.angularjs.org/1.4.0/docs/api/ng/filter/limitTo
#4
31
Say you have a list of items
, you can do it like this:
如果你有一个项目列表,你可以这样做:
<div ng-repeat="item in items" ng-if="$index >= myIndex">
{{item.Name}}
</div>
#5
4
I would create a function that returns the desire subarray. This can go in the controller or the link function of a directive.
我将创建一个返回desire子数组的函数。这可以进入控制器或一个指令的链接函数。
<span ng-repeat="item in getSubArray(0, 4)">...</span>
And the function
和功能
$scope.getSubArray = function (start, end) {
return array.slice(start, end);
}
#6
2
This worked for me:
这工作对我来说:
<element ng-repeat="round in view.rounds track by $index">
<span>{{$index + 1}}</span>
</element>
#7
2
This could do the trick...
这可以达到目的……
<div ng-repeat="(i, item) in items">
<p>{{i+1}}</p>
</div>
#8
0
Here is the result of what I was looking for:
这是我寻找的结果:
angular.module('starter.filters', []).filter('startFrom', function() {
return function(input, start) {
if(input) {
start = +start; //parse to int
appended = input.slice(0,start);
initialArray = input.slice(start);
finalArray= initialArray.concat(appended);
return finalArray;
}
return [];
}
});
thanks @emanuel
由于@emanuel