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?

user3214545
  • 2,103
  • 3
  • 20
  • 26

1 Answers1

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>
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234