我正在使用AngularJS在表上显示一些数据,为此,我正在使用ng-repeat来获取对象并在表单元格中显示其属性的值。

对象结构是这样的:

{
  ...
  ...
  "containerKey" : {
        "parentKey" : {
           "childKey1" : "value",
           "childKey2" : "value",
           "childKey3" : "value
        }
  }
}


ng-repeat显示表格

<table ng-repeat="key in containerKey">
   <tr>
    <!-- This should display the value of the key (in my case it is the name of a day in the week, but it displayed the entire object inside key.parentKey, which is expected -->
    <th id="day">{{ key.parentKey }}</th> <!-- I would like for this to show the value of the key, in my case that would be the name of the day, like "monday" -->

   </tr>
   <tr>
     <td>{{ parentKey.childKey1 }}</td>
   </tr>
   <tr>
     <td>{{ parentKey.childKey2 }}</td>
   </tr>
   <tr>
     <td>{{ parentKey.childKey3 }}</td>
  </tr>
</table>


如何在表格单元格中仅显示parentKey的键值?考虑到我正在使用ng-repeat显示多行,并且每一行都包含包含天(parentKey)的ng-repeat

澄清:

我希望带有<th>id="day"元素显示parentKey键的文本。实际上,th元素的内容应为parentKey(字符串parentKey),而不是parentKey的值。

最佳答案

您是否尝试过更改ng-repeat以使用对象表示法:

<table ng-repeat="(key, value) in containerKey">


然后,您应该可以访问该密钥。

09-25 18:42