Last Updated: February 25, 2016
·
1.087K
· marbio

A for loop with range in AngularJS

Un metodo utile per creare un range numerico per specificare ad esempio una data di nascita in un elemento <select/> è il seguente:

Per prima cosa creiamo un filtro contenente il range dei numeri necessari nella nostra applicazione angular:

MyApp.filter( 'agerange', function(){
    return function(input, total) {
        total = parseInt(total);
        for (var i=0; i<total; i++)
          input.push(i);
        return input;
      };
  });

Quindi nella nostra vista

<select id="year_age">
    <option ng-repeat="n in [] | agerange:100">{{ 1910 + n }}</option>
</select>

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

Angular apps often share the page with analytics and payment scripts that are not Angular modules. Those scripts execute on the same origin as the framework.

Security insights from Cside