如何在AngularJS中使用ng-repeat遍历键和值? [英] How to iterate over the keys and values with ng-repeat in AngularJS?

查看:76
本文介绍了如何在AngularJS中使用ng-repeat遍历键和值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的控制器中,我有如下数据: $scope.object = data

In my controller, I have data like: $scope.object = data

现在此数据是包含json中的键和值的字典.

Now this data is the dictionary with keys and values from json.

我可以使用模板中的object.name访问该属性.有什么办法可以遍历键并将它们显示在表格中吗?

I can access the attribute with object.name in the template. Is there any way that I can iterate over the keys as well and display them in table like

<tr><td> {{key}} </td> <td> data.key </td>

数据是这样的

{
    "id": 2,
    "project": "wewe2012",
    "date": "2013-02-26",
    "description": "ewew",
    "eet_no": "ewew",
}

推荐答案

怎么样:

<table>
  <tr ng-repeat="(key, value) in data">
    <td> {{key}} </td> <td> {{ value }} </td>
  </tr>
</table>

此方法在文档中列出: https://docs.angularjs.org/api/ng/directive/ngRepeat

This method is listed in the docs: https://docs.angularjs.org/api/ng/directive/ngRepeat

这篇关于如何在AngularJS中使用ng-repeat遍历键和值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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