角度数组不会显示嵌套数组

Angular array won't show nested array

本文关键字:数组 显示 嵌套      更新时间:2023-09-26

我试图在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我什么也得不到。为什么?

这是一个带有重现问题的 plunker:http://plnkr.co/edit/AtqpXogmItdSupEZGt7R?p=preview

使用 ng-options

<select class="form-control" ng-model="selected_category" 
ng-options="category as category.name for category in categories">
</select>