Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用AngularJS对包含@attributes的嵌套JSON数据进行排序_Javascript_Json_Angularjs_Angularjs Ng Repeat - Fatal编程技术网

Javascript 使用AngularJS对包含@attributes的嵌套JSON数据进行排序

Javascript 使用AngularJS对包含@attributes的嵌套JSON数据进行排序,javascript,json,angularjs,angularjs-ng-repeat,Javascript,Json,Angularjs,Angularjs Ng Repeat,我试图用两种不同的属性对嵌套的JSON数据进行排序:人口和平方英里 以下是我迄今为止所取得的成就 我认为问题在于ng repeat@attributes <tr ng-repeat="state in data.states.state"['@attributes']"|orderBy:orderByField:reverseSort"> 如何解决此问题?使用,您可以投影数据,以便更容易使用和删除@attributes属性: var app = angular.module(

我试图用两种不同的属性对嵌套的JSON数据进行排序:人口和平方英里

以下是我迄今为止所取得的成就

我认为问题在于ng repeat@attributes

<tr ng-repeat="state in data.states.state"['@attributes']"|orderBy:orderByField:reverseSort">

如何解决此问题?

使用,您可以投影数据,以便更容易使用和删除
@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">