自动在下拉框中选择已保存的值

时间:2014-11-05 12:18:22

标签: angularjs

 <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上自动选择已保存的值? 在这里,我保存了&#34; Team1&#34;在DB(字符串字段)上。此下拉列表没有任何&#34;值归档&#34;与它相关联。只有文本字段为Team1,Team2,...

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

4 个答案:

答案 0 :(得分:0)

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

以下内容应该有效ng-options="a.TeamName as a.TeamName for a in Event.Item.TeamNamesList"

编辑:

这样,指令将根据ng-model知道选择哪个值。

答案 1 :(得分: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>

答案 2 :(得分: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/

答案 3 :(得分: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属性的值与id列表中任意城市的cities的{​​{1}}相同,那么它就是&#39} ;将在加载时自动选择。

  

以下是Plunker:   http://plnkr.co/edit/1EVs7R20pCffewrG0EmI?p=preview

有关详细信息,请参阅源文档: https://code.angularjs.org/1.3.15/docs/api/ng/directive/select

相关问题