如何重复“键"?在ngRepeat中仅一次(AngularJS) [英] How to repeat "key" in ngRepeat one time only (AngularJS)

查看:116
本文介绍了如何重复“键"?在ngRepeat中仅一次(AngularJS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题出在后端/数据库的JSON数组响应中.我正在以正确的json格式获取数据库的响应:

The problem is leaving in the JSON Array response from the Backend/DB. I'm getting the response of the database in the correct json format:

[
  0: {
       "Grade": 100,
       "AB001": 1,
       "AB002": 0,
       "AB003": 9,
       "AB004": 5
     },
  1: {
       "Grade": 98,
       "AB001": 3,
       "AB002": 0,
       "AB003": 0,
       "AB004": 0
     }
...
] (10 objects as result)

因此,当您单击GET请求的响应时,将显示在Firebug控制台中.为了检索用双引号表示的键,我在视图中使用了ngRepeat指令,如下所示:

Thus displayed in the Firebug console, when you click the Response of the GET Request. To retrieve the keys who are represented in double quotes I've used the ngRepeat directive in my view like following:

<thead>
  <tr ng-repeat="d in data">
      <th ng-repeat="(key, value) in d">
          {{key}}
      </th>
  </tr>
</thead>
...

唯一的问题是,密钥重复了10次.但是我想重复一次按键,这意味着例如Grade按键在th标签中只是一次,依此类推.

Only the problem is, that the key gets repeated 10 times. But I want to repeat the keys one time that means for example the key Grade is only one time in a th-tag and so on..

我该如何实施?我已经用angular的forEach()进行了尝试,但这不是解决方案.

How can I implement this? I've tried it with angular's forEach() but it wasn't a solution.

推荐答案

如果数组中每个对象的键完全相同,则可以使用以下方法实现:

If you have the exact same keys in each object in the array, you could achieve this with:

<thead>
  <tr>
    <th ng-repeat="(key, value) in data[0]">
        {{key}}
    </th>
  </tr>
</thead>

在您的代码段中,您正在执行一个双循环,列出了数组中每个元素的每个键.

In your snippet you are doing a double loop, listing each key, for each element in the array.

这篇关于如何重复“键"?在ngRepeat中仅一次(AngularJS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆