[英]How do I set select default option in angular?
所以我有从服务器检索的json数据,如下所示:
$scope.StateList = {"States": [
{
"Id": 1,
"Code": "AL",
"Name": "Alabama"
},
{
"Id": 2,
"Code": "AK",
"Name": "Alaska"
},
{
"Id": 3,
"Code": "AZ",
"Name": "Arizona"
},
{
"Id": 4,
"Code": "AR",
"Name": "Arkansas"
}]}
$scope.Address = {"Address": {
"Address1": "123 Maple" ,
"Address2": null,
"City": "Tempe",
"State": "AZ",
"ZipCode": null,
"Country": null,
"Id": 0,
"Latitude": 0,
"Longitude": 0
}}
我设置了一个如下所示的选择:
<select ng-model="Address.State">
<option ng-repeat="template in StateList.States">{{template.Code}}</option>
</select>
我需要能够将Address.State字段绑定到所选的值,并将其作为select选项的初始值。 我无法确定这一点,并感谢任何能够向我展示我所缺少的东西的人。 这是一个可以使用的实时版本的插件 。
您可以做的是使用ng-options指令和ngModel,让angular为您创建选项。 尝试这种方式:-
<select ng-model="Address.Address.State"
ng-options="state.Code as state.Name for state in StateList.States"></select>
基本上,这使用语法
“选择”作为“数组”中“值”的“标签”
您可以按照PSL的建议使用ng-options,也可以使用options的value属性
<option ng-repeat="template in StateList.States"
value="{{template.Code}}">{{template.Code}}</option>
最好使用专门为选择下拉列表提供的ng-options
绑定,而不是提供迭代的ng-repeat
。 下面的标记将提供默认文本和下拉选项
<select ng-model='Address.State' ng-options="template.Code for template in StateList.States" >
<option value=""> Select one </option>
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.