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

本文翻译自: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> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值