我有一个像下面的关联数组,我想用我的下拉列表绑定:
{
"Item1": [
{
"title": "Item1",
"choices": [
"Egg",
"burger",
]
}
],
"Item2": [
{
"title": "Item2",
"choices": [
"Pizza",
"Rice",
]
}
]
}
我正在尝试基于此关联数组绑定下拉列表,但问题是它显示为object object
。
我想在下拉列表中显示每个项目的标题:
Item1
Item2
我试图从下面的SO问题中引用,但它没有成功:
Angularjs ng-options with an array of key-pair
var app = angular.module("myApp", []);
app.controller("MyController", function ($scope) {
$scope.item=
{
"Item1": [
{
"title": "Item1",
"choices": [
"Egg",
"burger",
]
}
],
"Item2": [
{
"title": "Item2",
"choices": [
"Pizza",
"Rice",
]
}
]
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<ul ng-app="myApp" ng-controller="MyController">
<select ng-model="myItem.title" ng-options="key as value for (key , value) in item">
<option value="">Select Items</option>
</select>
</ul>
答案 0 :(得分:2)
应该是这样的:
ng-options="value as key for (key , value) in item">
var app = angular.module("myApp", []);
app.controller("MyController", function ($scope) {
$scope.item=
{
"Item1": [
{
"title": "Item1",
"choices": [
"Egg",
"burger",
]
}
],
"Item2": [
{
"title": "Item2",
"choices": [
"Pizza",
"Rice",
]
}
]
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<ul ng-app="myApp" ng-controller="MyController">
<select ng-model="myItem.title" ng-options="value as key for (key , value) in item">
<option value="">Select Items</option>
</select>
{{myItem.title}}
</ul>