William Cézar William Cézar - 5 months ago 10
AngularJS Question

Fill a Select field with a Json call back using AngularJs

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 ?

Answer

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>