本文共 2359 字,大约阅读时间需要 7 分钟。
本文翻译自:
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",}
参考:
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' ]
How about: 怎么样:
{ {key}} | { { value }} |
This method is listed in the docs: 此方法列在文档中: :
If you would like to edit the property value with two way binding: 如果您想使用双向绑定编辑属性值:
{ {key}}
Here's a working example: 这是一个有效的例子:
{ {key}} : { {value}}
edited 编辑
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; } });
Total Items: { {toDoListCntrlAs.toDoListItems | propsCounter}} Enter todo Item: { {toDoListCntrlAs.toDoEntry}}{ {$index+1}} : { {key}} : Title = { { prop.title}} : Status = { { prop.completed}}
转载地址:http://ijcnb.baihongyu.com/