我试图在AngularJS
中显示嵌套数组。但是,我无法让它发挥作用。
控制器:
$scope.selected_category = [];
$scope.categories = [];
$scope.getCategories = function() {
ItemService.getCategories().success(function(categories) {
$scope.categories = categories;
});
}
$scope.getCategories();
表单设置$scope.selected_category
<select class="form-control" ng-model="selected_category">
<option ng-repeat="category in categories" value="{{ category }}">{{ category.name }}</option>
</select>
所以...现在打印{{ selected_category }}
显示内部带有subcategory
数组的预期数组:
{
"id": "1",
"name": "clothing",
"subcategories": [
{
"id": "1",
"name": "jackets",
"item_category_id": "1"
},
{
"id": "2",
"name": "jeans",
"item_category_id": "1"
},
{
"id": "3",
"name": "sweaters",
"item_category_id": "1"
}
]
}
然而,当我尝试selected_category.subcategories
时,我什么都没得到。那是为什么?
这是一个重现问题的掠夺者:http://plnkr.co/edit/AtqpXogmItdSupEZGt7R?p=preview
答案 0 :(得分:2)
使用ng-options
:
<select class="form-control" ng-model="selected_category"
ng-options="category as category.name for category in categories">
</select>