我有一个分配给角度模型的JSON字符串,用于循环并在UI上显示键和值。
<tr ng-repeat="(key,value) in Profile>
<td>{{key}} :</td>
<td>{{value}}</td>
</tr>
除了键的顺序与模型或不相同外,这是有效的 我的模型如下所示
"Profile": {
"UserID": 1234,
"User Name": "SomeName",
"Email Address": "someone@yahoo.com",
"Age": 29,
"Occupation": ""
}
输出是,我想以与JSON字符串相同的顺序获得它,我该如何实现它?
Age :29
Email Address : someone@yahoo.com
Occupation :
UserID : 1234
User Name : SomeName
答案 0 :(得分:1)
你需要一个解决方法,因为对象是无序的,如question所述:
以下是针对您的示例所表达的解决方法:
或jsFiddle:
<强> HTML 强>
<div ng-controller="MyCtrl">
<table>
<tr ng-repeat="key in keys(Profile)">
<td>{{key}}: {{Profile[key]}}</tr>
</table>
</div>
<强>的Javascript 强>
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": ""
};
});