2013-11-15 69 views
0

我想在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" 
    } 
    ] 
} 

Hooowever,當我嘗試做selected_category.subcategories我什麼都沒得到。這是爲什麼?

下面是與再現的問題plunker:http://plnkr.co/edit/AtqpXogmItdSupEZGt7R?p=preview

+1

在你plunker你指定'category.name'而不是實際的類對象'selected_category' –

回答

2

使用ng-options

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