user99999
user99999

Reputation: 2024

Getting key's name inside ng-repeat

I have the following structure:

$scope.data = [
    {
         'category1': {
               'type1': {
                    '2015-11-01': 1,
                    '2015-11-02': 3,
                    '2015-11-03': 2
               },
               'type2': {
                    '2015-11-01': 0,
                    '2015-11-02': 0,
                    '2015-11-03': 1
               }
         },
         'category2': {
               'type1': {
                    '2015-11-01': 0,
                    '2015-11-02': 1,
                    '2015-11-03': 0
               }
         }
    }
];

And I want to create a table like this out of it:

Category1:

-     2015-11-01 2015-11-02 2015-11-03
type1 1          3          2
type2 0          0          1

Category2:

-     2015-11-01 2015-11-02 2015-11-03
type1 0          1          0
type2 0          0          0

How can I do that?


I've been thinking about something like:

<div ng-repeat="row in data">
   {{row.key}}
   <table>
      <tr>
         <th>-</th>
         <th ng-repeat="date in row[0][0]">{{date.key}}</th>
      </tr>
      <tr ng-repeat="stat in $index">
         <td>row[$index]</td>
      </tr>
</div>

But it of course does not work, as I couldn't find any info about accessing key's name in angular's ng-repeat directive.

How can I do all of that?

Upvotes: 0

Views: 530

Answers (1)

deceze
deceze

Reputation: 522085

It is possible to get ngRepeat to iterate over the properties of an object using the following syntax:

<div ng-repeat="(key, value) in myObj"> ... </div>

https://docs.angularjs.org/api/ng/directive/ngRepeat

Upvotes: 1

Related Questions