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

前端 未结 6 1318
南笙
南笙 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:52

    Create a custom filter, for example:

    filter('modulo', function(){
      return function (arr, div, val) {
          return arr.filter(function(item, index){
              return index % div === (val || 0);
          })
      };
    });
    

    Then change the ng-repeat to:

    Or filter by (index % 3 === 1) like:

    http://jsfiddle.net/Tc34P/2/

提交回复
热议问题