如何使用 ng-options 和 ng-select with json

how to use ng-options and ng-select with json

有人可以解释一下当我有以下 json:

时如何使用 ng-options
{
"key1":
    {
        "name":"test1",
        "code":"horizontal",
        "fields":[
            {
                "type":"email"
            },
            {
                "type":"text"
            }
            ]
    },
"key2":
    {
        "name":"test2",
        "code":"vertical",
        "fields":[
            {
                "type":"emai"
            },
            {
                "type":"text"
            }
        ]
    }
}

然后我尝试像这样创建 select

<select name="cert" id="cert" ng-options="item as item[paramm] for item in listcert track by $index"></select>

其中 "paramm" = json 中的 $key。

我想看这样的东西

<select>
  <option value="horizontal" label='horizontal'>test1</option>
  <option value="vertical" label='vertical'>test2</option>
</select>

我不知道它是如何工作的。请帮助...

这是您要找的吗?这里的技巧是你的数据不是数组格式

var data = {
    "key1": {
            "name":"test1",
            "code":"horizontal",
            "fields":
                [{
                    "type":"email",
                },{
                    "type":"text",
                }]
        }, "key2": {
            "name":"test2",
            "code":"vertical",
            "fields":
                [{
                    "type":"emai",
                },{
                    "type":"text",
                }]
        }
}

angular.module("app", [])
        .controller("MainController", MainController);

function MainController() {
  var vm = this;
  vm.selected = {};
  vm.dataArray = [];
  
  angular.forEach(data, function(value, key) {
    vm.dataArray.push(value);
  }, data);
  
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="MainController as main">
  <select ng-options="value as value.code for value in main.dataArray track by value.name" ng-model="selected"></select>

<h3>Selected value:</h3>
<pre>{{selected | json}}</pre>
</div>

这可能适合你

     <select>
        <option ng-repeat="(key, value) in listcert" value="{{value.code}}" >{{value.name}}</option>
</select>