How to iterate over the keys and values with ng

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

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

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>

The data is like this

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

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


如果您想用双向绑定编辑属性值:

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

我不认为这样做有角度的内建函数,但可以通过创建一个包含所有标题名称的独立范围属性来完成此操作,并且可以像下面这样自动填充此属性:

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

$scope.objectHeaders = [];

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

// Output: [ 'foo', 'bar' ]
链接地址: http://www.djcxy.com/p/83834.html

上一篇: 检测用户向下滚动或在jQuery中向上滚动

下一篇: 如何用ng迭代键和值