Last Updated: February 25, 2016
·
1.108K
· 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?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside