6bt8iw
Last Updated: February 25, 2016
·
4.915K
· glebm
Profile

ng-repeat for loop

ng-repeat doesn't have support for numeric iterators, but we can add a range filter a la ruby:

module.filter 'toRange', ->
  (input) ->
    switch (input.length)
      when 1
        [lowBound, highBound] = [0, +input[0] - 1]
      when 2
        [lowBound, highBound] = [+input[0], +input[1]]
    i = lowBound
    result = []
    while i <= highBound
      result.push i
      i++
    result

Now we can iterate. 5 times:

div ng-repeat="i in [5] | toRange" {{i}} 

From 1 to 3:

div ng-repeat="i in [1, 3] | toRange" {{i}}

http://plnkr.co/edit/Er9iEBoHzsSs7xqBoHka?p=preview

Say Thanks
Respond