How to use ng-repeat with filter and $index?

前端 未结 6 1321
南笙
南笙 2020-12-08 07:33

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)\"
         


        
6条回答
  •  长情又很酷
    2020-12-08 07:56

    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/

    .filter('myFilter', function(){
        return function(data, parameter){
            var filtered=[];
            for(var i=0;i

    and then call it like this :

    ng-repeat='item in items | myFilter:3'
    

    (I added extra complexity with a parameter, if you want to change it quickly to even numbers for example)

    Have fun

    ++

提交回复
热议问题