[英]Angular js ng-options not able to set the value
I have started playing around with angular js and here is the problem with ng-option
. 我已经开始使用angular js,这是
ng-option
的问题。
I have a below DOM structure, where i have used ng-option
inside ng-repeat
. 我有一个下面的DOM结构,其中我在
ng-repeat
使用了ng-option
。 And i'm declaring Main model in ng-init
我在
ng-init
声明Main模型
<div ng-init='Mainmodel="{"SelectedValue": "value1",
"dummy" : [
{
"name" : "first attribute",
"description" : "attribute first description",
"values" : [{"id":"value1", "Name":"name1"}, {"id":"value2", "Name":"name2"}]
},
{
"name" : "second attribute",
"description" : "attribute second description",
"values" : [{"id":"value1", "Name":"name1"}, {"id":"value2", "Name":"name2"}]
}
]" >
<div ng-repeat="model in Mainmodel">
<select ng-model="SelectedValue" ng-options="item.id as item.Name for item in Mainmodel.dummy.values">
<option value=""> Please select </option>
</div>
</div>
The problem is, even thought the model SelectedValue
is set to value1
the dropdwon is not selecting the model value. 问题是,即使认为模型
SelectedValue
设置为value1
,dropdwon也不会选择模型值。 Dropdown is selected with Please select
. 已选择下拉菜单,
Please select
。
Can any one please tell where i'm doing wrong. 谁能告诉我我做错了什么。 As per my knowledge dropdwon will be selected with
ng-model
value. 据我所知,dropdwon将被选择
ng-model
值。
Note : The above model structure is not accurate. 注意:以上模型结构不准确。 Model is Valid dropdown is loading with all values.
“模型有效”下拉列表将加载所有值。
The problem is in your ng-options
. 问题出在您的
ng-options
。 You are using item.id as item.Name for item in Mainmodel.values
but item
does not have a property id
or Name
. 您正在使用
item.id as item.Name for item in Mainmodel.values
但item
没有属性id
或Name
。 It has name
, description
and values
. 它具有
name
, description
和values
。 You need to iterate over the items in values
. 您需要遍历
values
的项目。
You'll need to work around this probably using a combination of ng-repeat
over ng-repeat
without ng-options
. 您可能需要结合使用
ng-repeat
和ng-repeat
而不使用ng-options
。
Something like: 就像是:
<div ng-repeat="model in Mainmodel">
<select ng-model="SelectedValue">
<option value="">Make a choice</option>
<div ng-repeat="item in Mainmodel.values">
<option ng-repeat="subitem in item.values" value="{{subitem.id}}">{{subitem.Name}}</option>
</div>
</div>
I am writing this from top of my mind. 我在脑海中写这本书。 I have not tested, only trying to give you an idea.
我还没有测试,只是想给你一个想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.