Ng-repeat by passing a number

2

I want to set a select whose options will be generated by a ng-repeat, but for this I want to pass a number, for example if I pass the number 5 in the variable will be set 5 options with values 1,2,3,4 , 5.

<md-select ng-disabled="!novoCadastro.curso" required ng-model="novoCadastro.periodo">
 <md-option ng-value=""></md-option>
 <md-option ng-value="periodo" ng-repeat="periodo in novoCadastro.curso.periodo">{{periodo}}</md-option>
</md-select>  

In my newCadastro.curso.periodo will be where I will declare a number.

    
asked by anonymous 07.11.2017 / 03:01

1 answer

2

It is not a good practice for value to be generated dynamically. I would like to create a list of dictionaries or tuples and indicate value and text .

// dicionario
novoCadastro.curso.periodo = [{1: 'Primeiro'}, {2: 'Segundo'}, ...];
// ou tupla
novoCadastro.curso.periodo = [(1, 'Primeiro'), (2, 'Segundo'), ...];

If you still want to generate the value from the loop counter, you can use $index ;

<md-option ng-value="$index+1" ng-repeat="periodo in novoCadastro.curso.periodo">{{periodo}}</md-option>

The $index starts counting to 0.

    
07.11.2017 / 13:32