如何在AngularJs中使用没有特定名称的ng-repeat [重复]

时间:2022-11-25 17:24:10

This question already has an answer here:

这个问题在这里已有答案:

I am developing website with AngularJS.

我正在使用AngularJS开发网站。

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

我从服务器上抓取了json数据。我需要在桌子上显示它。

Json data format is

Json数据格式是

  { 
    {"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.

问题是每个括号的名称; a,b,c是来自服务器的动态数据,以便服务器人员可以更改名称。

There is my psedocode

有我的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}}

它不起作用。我想我知道它不起作用。但我不知道如何在不知道json数组名称的情况下进行更改。由于a,b,c是动态的,我不能只输入{{jsonObj.a}} {{jsonObj.b}}

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

我希望有人能帮助我解决这个问题。谢谢

2 个解决方案

#1


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}}

这意味着只需执行{{key}}或{{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

有关ng-repeat可以采取的不同形式的详细信息,请参阅https://docs.angularjs.org/api/ng/directive/ngRepeat

#2


0  

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

我想你的意思是你想要基于json数据的行和单元格。

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

这是一个小提琴

#1


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}}

这意味着只需执行{{key}}或{{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

有关ng-repeat可以采取的不同形式的详细信息,请参阅https://docs.angularjs.org/api/ng/directive/ngRepeat

#2


0  

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

我想你的意思是你想要基于json数据的行和单元格。

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

这是一个小提琴