Javascript “如何重复”;“关键”;仅重复一次(AngularJS)

Javascript “如何重复”;“关键”;仅重复一次(AngularJS),javascript,angularjs,angularjs-ng-repeat,ng-repeat,angularjs-controller,Javascript,Angularjs,Angularjs Ng Repeat,Ng Repeat,Angularjs Controller,问题是来自后端/DB的JSON数组响应中存在漏洞。我得到了正确json格式的数据库响应: [ 0: { "Grade": 100, "AB001": 1, "AB002": 0, "AB003": 9, "AB004": 5 }, 1: { "Grade": 98, "AB001": 3, "AB002": 0, "AB003": 0, "

问题是来自后端/DB的JSON数组响应中存在漏洞。我得到了正确json格式的数据库响应:

[
  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指令,如下所示:

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

{{key}}
...
唯一的问题是,这个键重复了10次。但是我想重复一次键,这意味着,例如,键
等级
在th标记中仅重复一次,以此类推


我如何实现这一点?我用angular的
forEach()
尝试过它,但它不是一个解决方案。

如果数组中的每个对象都有完全相同的关键点,可以通过以下方法实现:

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

{{key}}

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

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

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

{{key}}

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

您可以使用AngularUI的unique过滤器(此处提供源代码:)并直接在ng选项(或ng repeat)中使用它

试试这个:

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

{{key}}

您可以使用AngularUI提供的独特的过滤器(此处提供源代码:)并直接在ng选项(或ng repeat)中使用它

试试这个:

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

{{key}}

这个答案可能会对你有所帮助

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

{{key}}

这个答案可能对你有帮助

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

{{key}}

这是一个很好的解决方案,但是当我的透视表得到一个带有
AB011
的新列时,它就不在旧报告中了。是否可以在函数中检查并在ngRepeat指令中实现它?我想它将与
track by function()
一起工作,不是吗?如果数组中的对象可能有不同的键,那么要获得完整的列表,您最好在response
success
回调中编写一个函数,该函数可以快速循环遍历每个对象的键,并将它们添加到跟踪数组中(如果它们不在其中)。然后,您将使用ng repeat来替代此跟踪数组中的值。嗯,好的,我会尝试。这是一个不错的解决方案,但当我的透视表获得一个带有
AB011
的新列时,它就不在旧报告中。是否可以在函数中检查并在ngRepeat指令中实现它?我想它将与
track by function()
一起工作,不是吗?如果数组中的对象可能有不同的键,那么要获得完整的列表,您最好在response
success
回调中编写一个函数,该函数可以快速循环遍历每个对象的键,并将它们添加到跟踪数组中(如果它们不在其中)。然后你会使用ng repeat来代替跟踪数组中的值。嗯,好的,我会试试。谢谢你的回答。如果它对你有效,你可以将它标记为答案:P感谢你的答案。如果它对你有效,你可以将它标记为答案:P