Sniipe
Sniipe

Reputation: 1306

Angular Select dropdown set selected value

<select ng-if="PF_Set.PF.length > 0">
   <option ng-if="PF_Set.showPleaseSelect && PF_Set.PF.length>1">Please Select</option>
   <option ng-repeat="PF in PF_Set.PF" ng-click="doSomething()">{{::PF.SelectedDisplayValue}}</option>
</select>

The list has values like SelectedDisplayValue and isSelected. Only one item in the list will have isSelected=true. Can I have this as the selected value in the dropdown?

Updated - still with issues

<select ng-model="PF_set.PF" ng-attr-id="{{item.Index + '_' + PF_set.Type}}" ng-if="PF_set.PF.length > 0 >                                                                       
   <option ng-selected="PF.isSelected" ng-repeat="PF in PF_set.PF">{{PF.isSelected}} : {{::PF.SelectedDisplayValue}}</option>
</select>

Updated - 1 minor issue remaining, need to add "please select" to top of list if none selected

removed the ng-model. This must have caused a lot of confusion with the ng-repeat

<select ng-attr-id="{{item.Index + '_' + PF_set.Type}}" ng-if="PF_set.PF.length > 0 >                                                                       
   <option ng-selected="PF.isSelected" ng-repeat="PF in PF_set.PF">{{PF.isSelected}} : {{::PF.SelectedDisplayValue}}</option>
</select>

Upvotes: 0

Views: 555

Answers (1)

Danylo Gudz
Danylo Gudz

Reputation: 2656

You can use ng-selected directive: example

<option ng-repeat="PF in PF_Set.PF" 
  ng-selected="PF.isSelected">
{{PF.SelectedDisplayValue}}
</option>

Upvotes: 1

Related Questions