AngularJS ng-repeat格式的多层JSON

时间:2019-02-20 02:41:40

标签: angularjs angularjs-ng-repeat traversal

我的数据如下:

[
  {
    "TravelerExtended": {
      "ExtendedInt_1": {
        "#text": "0",
        "Label": "Do you require the hotel has a spa or fitness center?:"
      },
      "ExtendedInt_2": {
        "#text": "0",
        "Label": "Do you prefer to be prechecked into your room?:"
      },
      "ExtendedInt_3": {
        "#text": "0",
        "Label": "Connecting/Adjoining rooms, when traveling with family?:"
      },

然后,我的$ scope是这样的:

define(function () {
    return function ($scope, $ticket, $person, $http, $el, $app) {
        // TODO

    $scope.is_loading = true;

    var params = {
        url: "http://10.100.1.98:81/myidapi.php",

        // Any other parameters will be sent as-is
        // to your remote web service.
        // For example, here we are sending an 'email' parameter:
        id: $ticket.id
    };

    $http.get('DP_URL/agent/misc/proxy', {params: params}).success(function(data) {
        $scope.is_loading = false;
        $scope.profiles = data;

    });    


    };

});

然后,当然,我有HTML输出试图使用各种NG函数:

<div ng-repeat="traveler in profiles">
        Extended Profile
    <div ng-repeat="profile in traveler.TravelerExtended"> 
        <li ng-repeat-"field in profile.ExtendedInt_1">
         {{field.label}} {{field.#text}}
        </li>
    </div>
</div>

我似乎无法弄清楚如何获得第三级数据。似乎初始数据加载格式不正确,或者我只是搞砸了ng-repeat函数。

2 个答案:

答案 0 :(得分:1)

<TextBlock Grid.Column="2" Text="{Binding SumAmount}" /> 适用于数组...在您粘贴的数据结构中,您只有一个数组(ng-repeat),并且其中有一个对象(profiles)还有3个对象(TravelerExtended)。

要遍历它们,您需要在单个ExtendedInt_1, ExtendedInt_2, ExtendedInt_3中工作(因为只有一个数组)。以下代码对您粘贴的数据结构进行遍历。

ng-repeat
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.records = [
    "Alfreds Futterkiste",
    "Berglunds snabbköp",
    "Centro comercial Moctezuma",
    "Ernst Handel",
  ]

  $scope.profiles = [{
      "TravelerExtended": {
        "ExtendedInt_1": {
          "text": "0",
          "Label": "Do you require the hotel has a spa or fitness center?:"
        },
        "ExtendedInt_2": {
          "text": "0",
          "Label": "Do you prefer to be prechecked into your room?:"
        },
        "ExtendedInt_3": {
          "text": "0",
          "Label": "Connecting/Adjoining rooms, when traveling with family?:"
        }
      }
    },
    {
      "TravelerExtended": {
        "ExtendedInt_1": {
          "text": "0",
          "Label": "2 Do you require the hotel has a spa or fitness center?:"
        },
        "ExtendedInt_2": {
          "text": "0",
          "Label": "2 Do you prefer to be prechecked into your room?:"
        },
        "ExtendedInt_3": {
          "text": "0",
          "Label": "2 Connecting/Adjoining rooms, when traveling with family?:"
        }
      }
    }
  ];

  /*
  for(var i=0; i<$scope.profiles.length; i++){
    console.log($scope.profiles[0].TravelerExtended);
    console.log($scope.profiles[0].TravelerExtended.ExtendedInt_1);
  }
  */

});

答案 1 :(得分:0)

阿克伯说的是对的。如果您需要在对象内部自动循环,可以执行以下操作:

$http.get('DP_URL/agent/misc/proxy', {params: params}).success(function(data) {
       $scope.is_loading = false;
       $scope.profiles = formatData(data);

   });    
formatData(dataObject) {
       var dataArray = [];
       angular.forEach(dataObject, function(value, key) {
               dataArray.push(value);
       });
       return dataArray;
   }