The code below show a dropdown list from using ng-repeat. When I edit this entity, I have to make them show a certain value selected already than just showing a whole list. I know that I can put selected keyword when I want to select a certain value. Then what if I used ng-repeat to populate a dropdown? How can I make a dropdown choose a certain value using the id?
<div ng-if="field.name=='ClienteleId'" class="input-group inputFill">
<select ng-disabled="defaultSaveButtons[$index]" class="form-control inputFill2"
ng-model="field.value" ng-options="clientele.id as clientele.name for clientele in dropdown.clienteleOptions | orderBy:'name' track by clientele.id"></select>
/div>
To set the value of a
<select>
in AngularJS, you need to change the variable that is set as itsng-model
to one of the values in your options.In your case, you have
field.value
as yourng-model
, and your options use thename
attribute as their label and theid
attribute as their value. To have one of the items indropdown.clienteleOptions
selected by default, you need to setfield.value
to theid
attribute of the corresponding entry indropdown.clienteleOptions
.Imagine your options and fields look like this:
In your controller somewhere, you would have something like this to have test 2 selected:
Or to make it more dynamic: