gpt4 book ai didi

javascript - Angular 数组不会显示嵌套数组

转载 作者:行者123 更新时间:2023-12-02 18:07:06 26 4
gpt4 key购买 nike

我正在尝试在 AngularJS 中显示嵌套数组。但是,我无法让它工作。

Controller :

$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

最佳答案

使用ng-options:

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

关于javascript - Angular 数组不会显示嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20003467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com