Using ng-repeat in a range

colkito
Hashdog Blog
Published in
1 min readMay 30, 2015

Sometimes we need to repeat an element X times.

Sadly, AngularJS doesn’t have a native behavior for that, but we can create a simple filter to add this functionality.

'use strict';angular.module('MYAPP')
.filter('range', function() {
return function(list, total) {
total = parseInt(total, 10);

for (var i = 0; i < total; i++) {
list.push(i);
}
return list;
};
});

Basic Usage:

<div ng-repeat="n in [] | range:X">
{{$index}} - This will repeat X times.
</div>

--

--

colkito
Hashdog Blog

Tech addict. Art lover. Passionate about the power of nature. 🧩 Decentralize all the thinks.