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-modelto one of the values in your options.In your case, you have
field.valueas yourng-model, and your options use thenameattribute as their label and theidattribute as their value. To have one of the items indropdown.clienteleOptionsselected by default, you need to setfield.valueto theidattribute 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: