ngOptions从AngularJS中的内部和外部数组绑定

时间:2016-09-15 02:38:07

标签: javascript angularjs arrays

我有以下对象数组,并希望将我的select选项与第一个数组项名称结合使用,而不是成功。

阵列

"Collections": [
                  {
                      "Name": "Collection1",
                      "Fields": [
                        {
                            "Name": "Field1"
                        },
                        {
                            "Name": "Field2"
                        },
                        {
                            "Name": "Field3"
                    }
                  ]
              },
              {
                  "Name": "Collection2",
                  "Fields": [
                    {
                        "Name": "Field1"
                    },
                    {
                        "Name": "Field2"
                    }
                  ]
              }
            ]

选择

<select ng-options=" collectionList.Name for collectionList in Collections" class="form-control" ng-model="selected">

预期选项

<option value="">Collection1.Field1</option>
<option value="">Collection1.Field2</option>
<option value="">Collection1.Field3</option>
<option value="">Collection2.Field1</option>
<option value="">Collection2.Field2</option>

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

你可以这样做,

 <select ng-model='theModel'>
    <optgroup ng-repeat='group in collection' label="{{group.Name}}">
        <option ng-repeat='veh in group.Fields'>{{veh.Name}}</option>
    </optgroup>
 </select>

WORKING DEMO