AngularJS ng-options create range

Your way works fine. Another option that came to my head is to use a filter, so you don’t have to pollute your controller with Range.

JS:

var myApp = angular.module('myApp', []);
myApp.filter('range', function() {
  return function(input, min, max) {
    min = parseInt(min); //Make string input int
    max = parseInt(max);
    for (var i=min; i<max; i++)
      input.push(i);
    return input;
  };
});

HTML:

<select ng-model="page" ng-options="n for n in [] | range:1:30"></select>

Example: http://jsfiddle.net/N3ZVp/1/

P.S. in your example in your main post, you didn’t put var in front of i. So i is declared as a global variable in your example.

Leave a Comment