本文翻译自:How to iterate over the keys and values with ng-repeat in AngularJS?

In my controller, I have data like: $scope.object = data 在我的控制器中,我有以下数据: $scope.object = data

Now this data is the dictionary with keys and values from json . 现在这个数据是带有json键和值的字典。

I can access the attribute with object.name in the template. 我可以在模板中使用object.name访问该属性。 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>

The data is like this 数据是这样的

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

#1楼

参考:https://stackoom.com/question/11TRK/如何在AngularJS中使用ng-repeat迭代键和值


#2楼

I don't think there's a builtin function in angular for doing this, but you can do this by creating a separate scope property containing all the header names, and you can fill this property automatically like this: 我不认为有角度的内置函数可以做到这一点,但你可以通过创建一个包含所有标题名称的单独范围属性来实现,你可以像这样自动填充这个属性:

var data = {foo: 'a',bar: 'b'
};$scope.objectHeaders = [];for ( property in data ) {$scope.objectHeaders.push(property);
}// Output: [ 'foo', 'bar' ]

#3楼

How about: 怎么样:

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

This method is listed in the docs: https://docs.angularjs.org/api/ng/directive/ngRepeat 此方法列在文档中: https : //docs.angularjs.org/api/ng/directive/ngRepeat


#4楼

If you would like to edit the property value with two way binding: 如果您想使用双向绑定编辑属性值:

<tr ng-repeat="(key, value) in data"><td>{{key}}<input type="text" ng-model="data[key]"></td>
</tr>

#5楼

Here's a working example: 这是一个有效的例子:

<div class="item item-text-wrap" ng-repeat="(key,value) in form_list"><b>{{key}}</b> : {{value}}
</div>

edited 编辑


#6楼

A todo list example which loops over object by ng-repeat : 一个todo列表示例,它通过ng-repeat循环遍历对象:

 var app = angular.module('toDolistApp', []); app.controller('toDoListCntrl', function() { var self = this; self.toDoListItems = {};// []; //dont use square brackets if keys are string rather than numbers. self.doListCounter = 0; self.addToDoList = function() { var newToDoItem = {}; newToDoItem.title = self.toDoEntry; newToDoItem.completed = false; var keyIs = "key_" + self.doListCounter++; self.toDoListItems[keyIs] = newToDoItem; self.toDoEntry = ""; //after adding the item make the input box blank. }; }); app.filter('propsCounter', function() { return function(input) { return Object.keys(input).length; } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <body ng-app="toDolistApp"> <div ng-controller="toDoListCntrl as toDoListCntrlAs"> Total Items: {{toDoListCntrlAs.toDoListItems | propsCounter}}<br /> Enter todo Item: <input type="text" ng-model="toDoListCntrlAs.toDoEntry"/> <span>{{toDoListCntrlAs.toDoEntry}}</span> <button ng-click="toDoListCntrlAs.addToDoList()">Add Item</button> <br/> <div ng-repeat="(key, prop) in toDoListCntrlAs.toDoListItems"> <span>{{$index+1}} : {{key}} : Title = {{ prop.title}} : Status = {{ prop.completed}} </span> </div> </div> </body> 

如何在AngularJS中使用ng-repeat迭代键和值?相关推荐

  1. 数据绑定如何在AngularJS中运行?

    本文翻译自:How does data binding work in AngularJS? How does data binding work in the AngularJS framework ...

  2. html 获取选中radio,如何在html中获取被选中的radio的值?

    匿名用户 1级 2017-12-01 回答 一般来说,就是使用遍历的方法,判断每个Radio是否被选中,如果是,再取其值. 1:找出网页中所有控件的name为"rdPort" 的控 ...

  3. python中where函数_如何在python中基于Where函数获取两列值

    问题是: 基于user_id列,我想得到rating和{}列的值.可能有多个条目具有相同的用户标识.我希望获取具有rating和product_id列值的所有用户记录,但对于用户未对电影进行评级的电影 ...

  4. piechart 文档 android,mpchart piechart:如何在android中显示特定的只有一个文本值

    试试这个. piechart = (PieChart) findViewById(R.id.piechart); piechart.setUsePercentValues(true); ArrayLi ...

  5. python写入csv文件中添加行_在python中为csv文件输出键添加值

    这是我的输入csv文件. client_ip listener_service listener_port 127.0.0.1 LMNO 123 ::1 PQAR 768 ::1 PQAR 128 1 ...

  6. php添加数组的键和值,php - 将php数组元素添加到数组中如何具有相同的键和值 - SO中文参考 - www.soinside.com...

    我有一个像这样的数组如何具有相同的gtin:Array ( [0] ( [status] => ERROR [message] => 'message error' [gtin] => ...

  7. 举例说明如何在Go中使用Context | Gopher Daily (2021.09.15) ʕ◔ϖ◔ʔ

    每日一谚:Concurrency is not just for doing more things faster. It's for writing better code. Go技术生态 那些没有 ...

  8. java中map可以为空吗_Java: Map里面的键和值可以为空吗?

    在Java中,Map里面的键和值可以为空吗?我们先来看一个例子: private static void TestHashMap() { // TODO Auto-generated method s ...

  9. 如何在AngularJS的ng-options中设置value属性?

    本文翻译自:How do I set the value property in AngularJS' ng-options? Here is what seems to be bothering a ...

最新文章

  1. 51nod1584加权约数和
  2. 64位userdata.dll丢失_有什么一劳永逸解决私钥丢失的方法吗?
  3. 《Java并发编程的艺术》之阻塞队列
  4. 中标麒麟共享win7打印机_win10与win7系统电脑通过局域网共享打印机的方法
  5. mate30 e pro和mate30 pro的区别_新出的华为mate40跟华为mate40pro手机有何区别呢?
  6. CF Round#436 div2
  7. oracle sql语句加减,实现四则运算的一条sql语句
  8. tomcat配置重定向_在Tomcat上配置SSL以及从HTTP到HTTPS的设置自动重定向的步骤
  9. 绘制带省份的中国地图
  10. linux下低格u盘,拯救U盘,一个靠谱的强制低格的工具
  11. oracle访问emp表,通过deptno查询Emp表中雇员信息(oracle)
  12. 基于Android手机近距离感应器的俯卧撑计数器
  13. C# Winform生成条码二维码
  14. 华为招收mba硕士么_为什么我放弃MBA以获得计算机科学硕士学位
  15. Ouroboros Snake POJ - 1392(数位哈密顿回路)
  16. 用正则表达式提取富文本内容(去除html,p,a等标签)
  17. 微信公众号支付从前端到后台(小白教程)
  18. SSM 项目 --------- 小米商城后台管理系统
  19. 工作Delay,应该吸取什么教训?(一)
  20. java课程表_用Java做个课程表(5)

热门文章

  1. android 文件下载 超简单
  2. 从URL输入到页面展现,过程中发生了什么?
  3. 第十、十一周项目-阅读程序,写出这些程序的运行结果(2)
  4. 与splash详解相关的
  5. Android中的PopupWindow详解
  6. 周三多管理学第七版pdf_考研(管理学)相对好考的211院校推荐
  7. 拒绝服务(DoS)理解、防御与实现
  8. MB51升级后输入框的名字显示数据字段名
  9. log4j用于读取.xml文件的出现了错误,类加载器.getResource(user.xml).getPath()返回路径空格变成了%20...
  10. 可行性nullpoj 2723 Get Luffy Out 2sat