2014-11-05 47 views
2
<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> 

如何自动选择已保存的db值? 在这里,我保存了“Team1”与DB(字符串字段)一样。此下拉列表中没有任何与其关联的“值域”。只有文本字段为Team1,Team2,...自动选择下拉框中已保存的值

编辑:在上面的设置中,我可以正确保存数据。但问题我已经是当它再次显示下拉框中的数据。任何帮助将不胜感激。

+0

的可能重复[如何在选择框中默认选项 - 角。 js](http://stackoverflow.com/questions/18194255/how-to-have-a-default-option-in-select-box-angular-js) – nightgaunt 2014-11-05 12:28:41

+0

@nightgaunt这不是一个重复的one.I需要从db中设置值。不是默认的o ne。下次用户下载时,它保存在数据库中的值应显示在下拉框中。 – Sampath 2014-11-05 13:01:02

回答

0

您需要通过ng-options属性指定使用哪个值。

以下应该工作ng-options="a.TeamName as a.TeamName for a in Event.Item.TeamNamesList"

编辑:

这样的指令将知道如何选择基础上,ng-model其价值。

+0

仍然值为0,1,2?为什么? – Sampath 2014-11-05 12:43:09

+0

但它工作? – shizik 2014-11-05 13:07:14

+0

我的错误,它仍然呈现为0,1,2。但是,如果指定'ng-options'的'as'部分,则指令将知道要使用哪个值。我也编辑了我的答案。 – shizik 2014-11-05 13:14:06

0

对于像[{"Selected":false,"Text":"Yugoslavia","Value":"244"},{"Selected":false,"Text":"Zambia","Value":"246"},{"Selected":false,"Text":"Zimbabwe","Value":"247"}]

数据,这是什么工作

<select 
    ng-model="paymentDetails.BeneficiaryCountry" 
    ng-options="country.Value as country.Text for country in paymentDetails.BeneficiaryCountryList"> 
    <option value=""></option> 
</select> 
+0

我的下拉列表只是一个字符串数组。不是一个复杂的对象。任何线索? – Sampath 2014-11-05 13:23:38

0

试试这个

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

你可以参考下面的jsfiddle

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

+0

但是,如果你检查你的小提琴,它显示值为0,1 ..这样的?使用F12键。 – Sampath 2014-11-05 12:46:55

0

在这里被完全工作示例中,关键是要使用track by选择ng-options像这样:

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

如果selectedCity上的角范围限定,并且它具有id属性具有相同值的如cities列表中任何城市的任何id,它将在加载时自动选择。

这里是Plunker此: http://plnkr.co/edit/1EVs7R20pCffewrG0EmI?p=preview

更多详情请参阅源文件: https://code.angularjs.org/1.3.15/docs/api/ng/directive/select