[英]How to loop through the 'key' value of an object with AngularJS?
I have the following data and HTML template (and other code in app.js obviously). 我有以下数据和HTML模板(显然还有app.js中的其他代码)。 The code in my "tbody" works perfectly, and displays a table like this:
我的“ tbody”中的代码可以完美运行,并显示如下表:
current output 电流输出
--------------------
| AAPL | 127 | 128 |
--------------------
| GOOG | 523 | 522 |
--------------------
| TWTR | 35 | 36 |
--------------------
Now I'm trying to loop through the 'keys' of the first object and display them in a 'thead' like so: 现在,我试图遍历第一个对象的“键”,并以“ thead”形式显示它们,如下所示:
desired output 期望的输出
--------------------
| Name | jan | feb |
--------------------
| AAPL | 127 | 128 |
--------------------
| GOOG | 523 | 522 |
--------------------
| TWTR | 35 | 36 |
--------------------
data 数据
$scope.data = [
{
"name": "AAPL",
"jan": "127",
"feb": "128"
},
{
"name": "GOOG",
"jan": "523",
"feb": "522"
},
{
"name": "TWTR",
"jan": "35",
"feb": "36"
}]
html HTML
<table>
<thead>
<tr>
<td ng-repeat="">{{}}</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="object in data">
<td ng-repeat="(a,b) in object">{{b}}</td>
</tr>
</tbody>
</table>
It has been pointed out that this question is a duplicate of another one, although, the other one is in plain JS, this one uses AngularJS. 有人指出,这个问题是另一个问题的重复,尽管另一个问题是纯JS,但是这个问题使用了AngularJS。
If you use underscoreJS (which is a very common and competent convenience library) then you can get the keys of the first object like this: 如果使用underscoreJS(这是一个非常常见且功能强大的便捷库),则可以像这样获得第一个对象的键 :
_.keys(data[0])
This presume that all elements in the array have the same keys & structure. 这假定数组中的所有元素都具有相同的键和结构。 Looks like it, so you should be safe.
看起来像,所以您应该很安全。
After including UnderscoreJS in your app, then you would call it from the template in the bracketed expression: 在您的应用中包含UnderscoreJS之后,您可以从方括号中的模板中调用它:
<td ng-repeat="">{{ _.keys(data[0])}}</td>
If your hard-up against a library, and you can stomach ES5, then you can use 如果您遇到图书馆问题,并且可以忍受ES5,则可以使用
Object.keys(data[0])
<td ng-repeat="">{{ Object.keys(data[0]) }}</td>
<td ng-if="data.length" ng-repeat="(a,b) in data[0]">{{a}}</td>
您只需要考虑键值始终以相同的顺序出现,否则表将以错误的顺序显示值。
You use use Angular's (key, value)
syntax. 您使用Angular的
(key, value)
语法。 However, I don't think this will work in your case because the order of iteration is random. 但是,我认为这不适用于您的情况,因为迭代顺序是随机的。
I think your best best is either to hardcode the keys in a $scope.headings
variable or to update the format of your data (if you have control over how it is generated) so that the order of keys is specified. 我认为最好的办法是对
$scope.headings
变量中的键进行硬编码,或者更新数据的格式(如果可以控制其生成方式),以便指定键的顺序。
@murid, Do you need this: @murid,您需要这个吗:
In script tag of head: 在head的脚本标签中:
var app = angular.module('myApp', []); app.controller('myCtrl', function ($scope) { $scope.data = [{ "name": "AAPL", "jan": "127", "feb": "128" }, { "name": "GOOG", "jan": "523", "feb": "522" }, { "name": "TWTR", "jan": "35", "feb": "36" }]; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="myCtrl"> <table> <thead> <tr> <td ng-repeat="">{{}}</td> </tr> </thead> <tbody> <tr ng-repeat="object in data"> <td ng-repeat="(a,b) in object">{{b}}</td> </tr> </tbody> </table> </div>
Hope this help. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.