0

I am developing website with AngularJS.

I grabbed the json data from the server. and I need to display it on the table.

Json data format is

  { 
    {"a" :1,"b": 2,"c":3}, 
    {"a" :3,"b": 4,"c":5}, 
    {"a" :1,"b": 2,"c":3}
  }

The problem is each bracket's names; a, b,c are the dynamic data from the server so that the server guy can change the name.

There is my psedocode

    <tr ng-repeat = "jsonObj in jsonArray"> 
     <td ng-repeat = "name in jsonObj">{{name}} </td>
  </tr>

it doesn't work. I think I know what it doesn't work. but I cant think of how to change without knowing the name of json array.
Since a, b,c are dynamic, i can't just type {{jsonObj.a}} {{jsonObj.b}}

I hope anyone can help me to fix this. Thank you

vusan
  • 5,221
  • 4
  • 46
  • 81
user1979727
  • 117
  • 1
  • 8

2 Answers2

5

Use the following format:

ng-repeat="(key,value) in myObj"

This means you can quite easily display the value and key separetly by just doing {{key}} or {{value}}

<tr ng-repeat="obj in jsonArray">
  <td ng-repeat="(key,val) in obj ">{{val}}</td>
</tr>

See https://docs.angularjs.org/api/ng/directive/ngRepeat for more details on the different forms ng-repeat can take

link64
  • 1,944
  • 1
  • 26
  • 41
0

I think what you mean is you want rows and cells based on json data.

This is very simple but you may just need an array like.

 $scope.items =  [ 
  {a:1,b: 2,c:3}, 
  {a:3,b: 4,c:5}, 
  {a:1,b: 2,c:3}
];

and not an object.

Here's a fiddle

Dylan
  • 4,703
  • 1
  • 20
  • 23