简体   繁体   English

Angular js ng-options无法设置值

[英]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.valuesitem没有属性idName It has name , description and values . 它具有namedescriptionvalues 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-repeatng-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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM