在我的控制器中,我有如下数据: 美元的范围。对象=数据

现在这个数据是json中包含键和值的字典。

我可以在模板中使用object.name访问属性。是否有任何方法,我可以遍历键以及显示他们在表一样

<tr><td> {\ fnhobo std \ fs48} </td>数据。key) < / td >

数据是这样的

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

当前回答

我不认为angular中有一个内置函数可以做到这一点,但你可以通过创建一个包含所有头文件名称的单独scope属性来做到这一点,你可以像这样自动填充这个属性:

var data = {
  foo: 'a',
  bar: 'b'
};

$scope.objectHeaders = [];

for ( property in data ) {
  $scope.objectHeaders.push(property); 
}

// Output: [ 'foo', 'bar' ]

其他回答

一个通过ng-repeat循环object的todo列表示例:

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>

    Use below code it is working to display your key and value here is key start with 1:
         <tr ng-repeat="(key,value) in alert_list" >
                   <td>{{key +1}}</td>
                   <td>{{value.title}}</td>
                 </tr>
Below is document link for it. 

https://docs.angularjs.org/api/ng/directive/ngRepeat

如何:

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

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

我们可以遵循以下过程来避免按字母顺序显示键值。

Javascript

$scope.data = {
   "id": 2,
   "project": "wewe2012",
   "date": "2013-02-26",
   "description": "ewew",
   "eet_no": "ewew",
};
var array = [];
for(var key in $scope.data){
    var test = {};
    test[key]=$scope.data[key];
    array.push(test);
}
$scope.data = array;

HTML

<p ng-repeat="obj in data">
   <font ng-repeat="(key, value) in obj">
      {{key}} : {{value}}
   </font>
</p>

我不认为angular中有一个内置函数可以做到这一点,但你可以通过创建一个包含所有头文件名称的单独scope属性来做到这一点,你可以像这样自动填充这个属性:

var data = {
  foo: 'a',
  bar: 'b'
};

$scope.objectHeaders = [];

for ( property in data ) {
  $scope.objectHeaders.push(property); 
}

// Output: [ 'foo', 'bar' ]