1

I have an object with an array:

 {
    People: [
        {
            label: 'label1',
            type: 'type1'
        },
        {
            label: 'label2',
            type: 'type2'
        }],
    Places: [
       {
            label: 'label1',
            type: 'type1'
        },
        {
            label: 'label2',
            type: 'type2'
        }]
}

I have an ng-repeat to create a list. I can bind to the properties inside People and Places but cannot bind to the name 'People' and 'Places'.

My html is something like this:

<div ng-repeat="category in vm.categories">
   <button>{{category}}</button>
    <ul>
      <li ng-repeat="subcategory in category">
        <a href="#"> {{subcategory.label}}</a>
      </li>
    </ul>
   </button>
 </div>

so subcategories are all fine but {{category}} doesn't return People/Places. How do I bind to the name of the array in the object?

1 Answer 1

1

Try this by using iterate over the keys and values with ng-repeat in AngularJS?

angular.module("test",[]).controller("testc",function($scope) {   
      $scope.categories = {
People: [
    {
        label: 'label1',
        type: 'type1'
    },
    {
        label: 'label2',
        type: 'type2'
    }],
Places: [
   {
        label: 'label1',
        type: 'type1'
    },
    {
        label: 'label2',
        type: 'type2'
    }]
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="test" ng-controller="testc">
   <div ng-repeat="(key, value) in categories">
   <button>{{key}}</button>
<ul>
  <li ng-repeat="subcategory in value">
    <a href="#"> {{subcategory.label}}</a>
  </li>
</ul>
   </button>
 </div>
</div>

Sign up to request clarification or add additional context in comments.

3 Comments

@user3214545 Could you please do mark as an answer if it works for you. :)
apologies for the delay, it wouldnt let me mark it for a few mins :)
No problem! Great & Thanks :)

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.