I want to use ng-repeat in Angular, while I only want to output some elements of the array. An Example:
ng-repeat="item in items | filter:($index%3 == 0)"
However, this does not work. Please tell me how to do this; only output exact index of elements.
In your code, filter apply on 'items' array, not on each array item, that's why it does not work as you expect.
Instead, you can use ng-show (or ng-if):
See: http://jsfiddle.net/H7d26
EDIT: Use ng-if directive if you do not want to add invisible dom elements:
All the precedent answers will work, but if you want to use a filter, you can also define it yourself, like in this fiddle : http://jsfiddle.net/DotDotDot/58y7u/
and then call it like this :
(I added extra complexity with a parameter, if you want to change it quickly to even numbers for example)
Have fun
++
What you need to do is find the index of "item" in $scope.items. See below
So a real world example (for others who come across this solution) would be.
The above example will get the correct position regardless of the sorting or filtering
I suggest filtering with a function:
And on the Controller:
An alternate approach to solving this creates a new "list" (I don't know the proper term in this context) in the
ng-repeat
. This new list can then be referred to within the scopeThe
ng-if
on thespan
wrapping the second half prevents the base text from being shown when the previous element is the last element of the list.Create a custom filter, for example:
Then change the
ng-repeat
to:Or filter by
(index % 3 === 1)
like:http://jsfiddle.net/Tc34P/2/