我正在尝试使用2个不同的属性,种群和平方米对嵌套的JSON数据进行排序。
这是我迄今取得的成就。
http://codepen.io/anon/pen/zxooMv
我认为问题出在ng-repeat @attributes ..
<tr ng-repeat="state in data.states.state"['@attributes']"|orderBy:orderByField:reverseSort">
我如何解决这个问题?
答案 0 :(得分:0)
使用array.map
,您可以投影数据,以便更轻松地使用并摆脱@attributes
属性:
var app = angular.module('app', []);
app.controller('MainCtrl', function($scope) {
$scope.orderByField = 'population';
$scope.reverseSort = false;
var data = {"states":{
"state":[{
"@attributes":{ "name":"ALABAMA","abbreviation":"AL","capital":"Montgomery","most-populous-city":"Birmingham","population":"4708708","square-miles":"52423","time-zone-1":"CST (UTC-6)","time-zone-2":"EST (UTC-5)","dst":"YES"}},{
"@attributes":{"name":"ALASKA","abbreviation":"AK","capital":"Juneau","most-populous-city":"Anchorage","population":"698473","square-miles":"656425","time-zone-1":"AKST (UTC-09)","time-zone-2":"HST (UTC-10)","dst":"YES"}}]}};
data.states.state = data.states.state.map(function(value) {
return value['@attributes'];
});
// at this stage the data variable will look like this:
//{
// "states": {
// "state": [
// {
// "name": "ALABAMA",
// "abbreviation": "AL",
// "capital": "Montgomery",
// "most-populous-city": "Birmingham",
// "population": "4708708",
// "square-miles": "52423",
// "time-zone-1": "CST (UTC-6)",
// "time-zone-2": "EST (UTC-5)",
// "dst": "YES"
// },
// {
// "name": "ALASKA",
// "abbreviation": "AK",
// "capital": "Juneau",
// "most-populous-city": "Anchorage",
// "population": "698473",
// "square-miles": "656425",
// "time-zone-1": "AKST (UTC-09)",
// "time-zone-2": "HST (UTC-10)",
// "dst": "YES"
// }
// ]
// }
//}
$scope.data = data;
});
现在在您的视图中,您可以简单地使用它:
<tr ng-repeat="state in data.states.state|orderBy:orderByField:reverseSort">