Json 使用ng repeat按插入顺序检索键
我有一个JSON字符串,分配给angular模型,用于循环并在UI上显示键和值Json 使用ng repeat按插入顺序检索键,json,angularjs,angularjs-directive,angularjs-ng-repeat,Json,Angularjs,Angularjs Directive,Angularjs Ng Repeat,我有一个JSON字符串,分配给angular模型,用于循环并在UI上显示键和值 <tr ng-repeat="(key,value) in Profile> <td>{{key}} :</td> <td>{{value}}</td> </tr> 输出是,我希望以与JSON字符串相同的顺序获得它,我如何实现它 Age :29 Email Address : someone@yahoo.com Occupati
<tr ng-repeat="(key,value) in Profile>
<td>{{key}} :</td>
<td>{{value}}</td>
</tr>
输出是,我希望以与JSON字符串相同的顺序获得它,我如何实现它
Age :29
Email Address : someone@yahoo.com
Occupation :
UserID : 1234
User Name : SomeName
您确实需要一种解决方法,因为对象是无序的,如下所述: 以下是为您的示例所表达的变通方法: 或: HTML
<div ng-controller="MyCtrl">
<table>
<tr ng-repeat="key in keys(Profile)">
<td>{{key}}: {{Profile[key]}}</tr>
</table>
</div>
<div ng-controller="MyCtrl">
<table>
<tr ng-repeat="key in keys(Profile)">
<td>{{key}}: {{Profile[key]}}</tr>
</table>
</div>
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', function ($scope) {
$scope.keys = function(obj){return obj? Object.keys(obj) : [];}
$scope.Profile = {
"UserID": 1234,
"User Name": "SomeName",
"Email Address": "someone@yahoo.com",
"Age": 29,
"Occupation": ""
};
});