0

I am trying to show a nested array in AngularJS. However, I can't get it to work.

Controller:

$scope.selected_category = [];
$scope.categories = [];

$scope.getCategories = function() {
    ItemService.getCategories().success(function(categories) {
        $scope.categories = categories;
    });
}

$scope.getCategories();

The form setting $scope.selected_category

<select class="form-control" ng-model="selected_category">
    <option ng-repeat="category in categories" value="{{ category }}">{{ category.name }}</option>
</select>

So... now printing {{ selected_category }} show the expected array with a subcategory array inside:

{
  "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, when I try to do selected_category.subcategories I get nothing. Why is that?

Here's a plunker with the reproduced problem: http://plnkr.co/edit/AtqpXogmItdSupEZGt7R?p=preview

1
  • 1
    In your plunker you're assigning category.name and not the actual category object to selected_category Commented Nov 15, 2013 at 14:48

1 Answer 1

2

Use ng-options:

<select class="form-control" ng-model="selected_category" 
ng-options="category as category.name for category in categories">
</select>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.