2015-01-02 28 views

回答

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"> 
相关问题