3
 <select ng-model="Event.Team" ng-options="a.TeamName for a in Event.Item.TeamNamesList" required="">

<option value="" disabled="" class="">-- Select Your Team --</option>
<option value="0">Team1</option>
<option value="1">Team2</option>
<option value="2">Team3</option></select>

How can I auto select the already saved value on db ? Here I saved the "Team1" as on DB(string field). This drop down does not have any "value filed" associated with it.Only text fields as Team1,Team2,...

EDIT : On above set-up where I can save the data properly.But the problem I have is when it shows that data again on drop down box.Any help would be much appreciated.

Sampath
  • 63,341
  • 64
  • 307
  • 441
  • possible duplicate of [How to have a default option in select box - Angular.js](http://stackoverflow.com/questions/18194255/how-to-have-a-default-option-in-select-box-angular-js) –  Nov 05 '14 at 12:28
  • @nightgaunt This is not a duplicate one.I need to set the value from db.Not the default one.The value it saved on the db should show on drop down box when user comes next time. – Sampath Nov 05 '14 at 13:01

4 Answers4

1

For data like [{"Selected":false,"Text":"Yugoslavia","Value":"244"},{"Selected":false,"Text":"Zambia","Value":"246"},{"Selected":false,"Text":"Zimbabwe","Value":"247"}]

this is what works

<select 
  ng-model="paymentDetails.BeneficiaryCountry"
  ng-options="country.Value as country.Text for country in paymentDetails.BeneficiaryCountryList">
    <option value=""></option>
</select>
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
0

You need to specify which value will be used via the ng-options attribute.

The following should work ng-options="a.TeamName as a.TeamName for a in Event.Item.TeamNamesList".

EDIT:

This way the directive will know which value to select based on the ng-model.

shizik
  • 910
  • 6
  • 16
  • Still values are coming as 0,1,2 ? Why ? – Sampath Nov 05 '14 at 12:43
  • My mistake, it still renders it as 0,1,2. However the directive will know with which value to work if you specify the `as` part of `ng-options`. I have also edited my answer. – shizik Nov 05 '14 at 13:14
  • Still no luck.My drop down is just a text filed.In other words string array.It's not a complex object. – Sampath Nov 05 '14 at 13:17
  • I would be helpful if you can provide a plunk, so we can better understand the whole page. – shizik Nov 05 '14 at 19:09
0

try this

   select ng-model="Event.Team" ng-options="a.TeamName as a.TeamName  for a in Event.Item.TeamNamesList" required="">

you can refer below jsfiddle

http://jsfiddle.net/n9rQr/20/

Nitu Bansal
  • 3,826
  • 3
  • 18
  • 24
0

Here is fully working example, the trick is to use track by in select ng-options like so:

<select ng-model="selectedCity"
        ng-options="city as city.name for city in cities track by city.id">
  <option value="">-- Select City --</option>
</select>

If selectedCity is defined on angular scope, and it has id property with the same value as any id of any city on the cities list, it'll be auto selected on load.

Here is Plunker for this: http://plnkr.co/edit/1EVs7R20pCffewrG0EmI?p=preview

See source documentation for more details: https://code.angularjs.org/1.3.15/docs/api/ng/directive/select

mikhail-t
  • 4,103
  • 7
  • 36
  • 56