Angularjs ng-options: Use object property as value instead of array index

2

In the code below, AngularJS ng-options renders the value but records the position in the array and not the value.

I get the following json:

[{"iduniforme":1,
  "nomeuniforme":"Uniforme 5 itens",
  "itensuniforme":[{...},{...},{...},{...},{...}]},
 {"iduniforme":2,
  "nomeuniforme":"Uniforme 7 itens",
  "itensuniforme":{...},{...},{...},{...},{...},{...},{...}]}]

Using Angularjs ng-options, I mount my select:

<div class="form-group">
   <label>Uniforme da Equipe</label>
   <select class="form-control" name="uniforme" id="uniforme"
   ng-model="uniforme" ng-options="x.nomeuniforme for x in listaUniformes">
   <option value="">Selecione o uniforme</option>
   </select>
</div>

I can render normally, ie the options appear correctly, however, ah but when I submit the form the amount recorded is the position within the array [0] or [1] and not the value in "nomeuniforme" in this case would be "Uniform 5 items" or "Uniform 7 items".

How do I resolve this? I need to register the value "Uniform 5 items" or "Uniform 7 items" in the base.

    
asked by anonymous 18.06.2015 / 16:20

2 answers

2

It did not work with your examples.

I was able to How to set the value property in AngularJS 'ng-options?

Adding track by, the code looks like this:

ng-options="x.nomeuniforme for x in listaUniformes track by x.nomeuniforme">

Originated by help. Thanks.

    
18.06.2015 / 22:51
0

Include the AS clause to help AngularJS determine the value / text pair to use. In your case, replace

<select class="form-control" name="uniforme" id="uniforme"
   ng-model="uniforme" ng-options="x.nomeuniforme for x in listaUniformes">

by

<select class="form-control" name="uniforme" id="uniforme"
   ng-model="uniforme" ng-options="x.iduniforme as x.nomeuniforme for x in listaUniformes">

If you want to reference the property IdUniforme , or

<select class="form-control" name="uniforme" id="uniforme"
   ng-model="uniforme" ng-options="x.nomeuniforme as x.nomeuniforme for x in listaUniformes">

If you want to reference the nomeuniforme property.

    
18.06.2015 / 17:12