0

I'm a pretty new in this area (one mounth) and I'm developing application with angularJs, I'm getting back Json requisition with a list, I have to put the name ({{empresa.name }}) when the user select I get the ID({{empresa.IdEmpresa}}).

I want use ng-repeat for that, something like (ng-repeat="empresa in empresas").

I'm getting the Json with the list from the JavaController, my Entity is named Empresa, I declare a empty object on angular Like that ( $scope.empresas = {}) and give the callback to this emprty object, right?!

My select field is like that

     <label>Empresa</label>
<select class="form-control">
 <option value="{{empresa.EmpresaId}}">{{empresa.name}}</option>
 </select>

how do I use repeat in this ?

2 Answers 2

2

Assuming your JSON data look like this

$scope.empresas = [
  { EmpresaId: 1, name: 'one' },
  { EmpresaId: 2, name: 'two' },
  { EmpresaId: 3, name: 'three' }
]

Apart from using ng-repeat on <options>, you can also utilise ng-options

<select ng-options="e.EmpresaId as e.name for e in empresas" ng-model="???"></select>
Sign up to request clarification or add additional context in comments.

6 Comments

I'll see the usage of ng-option on documentation of AngularJs , i never see that directive . I forget that the the model that will receive that selection is ng-model="colaborador.empresa". So will be something like that : <select ng-options="e.EmpresaId as e.name for e in empresas" ng-model="colaborador.empresa">{{empresa.name}}</select> or I dont need the {{empresa.name}}?
Take away the {{empresa.name}} will do
My Json its something like that, but have more atributes that I'll not use. the ng-options will make the restrictions for me?
If you prefer receiving the whole object instead of just the id in ng-model (judging from your ng-model name), you can try taking away e.EmpresaId as as well.
I just have to Show the empresa.name for the user , and give to the ng-model="colaborador.empresa" , the empresa.IdEmpresa.
|
0

You can use ng-repeat="empresa in empresas" on the <option> tags.

Here's an example:

<select>
    <option ng-repeat="empresa in empresas" value="{{empresa.EmpresaId}}">{{empresa.name}}</option>
</select>

Controller Code

$scope.empresas = [
  { EmpresaId: 101, name: 'India' },
  { EmpresaId: 102, name: 'US' },
  { EmpresaId: 103, name: 'UK' }
];

2 Comments

<option ng-repeat="empresa in empresas" value="{{empresa.EmpresaId}}">{{empresa.name}}</option> </select> like that?
Add a <select> in the beginning as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.