缺少深层次的数组内容

时间:2015-12-16 17:01:07

标签: arrays json ionic-framework ionic

我使用ionic来获取位于here数组内的JSON对象内的数组内容。这是整个JSON对象的一部分:

{
"services": [
{
  "id": 3,
  "info": null,
  "name": "singer",
  "owner": 1,
  "packages": [
    {
      "id": 1,
      "package": "5 hours",
      "price": 400
    },
    {
      "id": 2,
      "package": "3 days",
      "price": 4000
    },
    {
      "id": 3,
      "package": "1 week",
      "price": 12000
    }
  ],
  "reviews": [],
  "type": "performer"
},
//...

包的数组应该有内容,但由于某种原因,数组是空的。这是控制器显示所需的东西:

.controller('ServiceCtrl', function($scope, Auth, $http, $state, $stateParams) {
var username = Auth.getUser();
$scope.userID = Auth.getId();

$http.get('http://eventoserver-ecinauce.rhcloud.com/' + username + '/services/' + $stateParams.serviceId)
.success(function (data, status, headers, config) {
    $scope.selectedService = data;
    $scope.serviceName = data.service[0].name;
    $scope.serviceType = data.service[0].type;
    $scope.ownerId = data.service[0].owner;
    $scope.packages = data.service[0].packages;
    console.log(data.service[0].packages.length);
})
.error(function (data, status, header, config) {
  $scope.services = data;
});
})

在Ionic中,包显示为

[]

为什么缺少数组内容?

1 个答案:

答案 0 :(得分:0)

我认为你的$http.get成功回调中有拼写错误。这是services而不是service

$http.get('http://eventoserver-ecinauce.rhcloud.com/' + username + '/services/' + $stateParams.serviceId)
.success(function (data, status, headers, config) {
    $scope.selectedService = data;
    $scope.serviceName = data.services[0].name;
    $scope.serviceType = data.services[0].type;
    $scope.ownerId = data.services[0].owner;
    $scope.packages = data.services[0].packages;
    console.log(data.services[0].packages.length);
})
.error(function (data, status, header, config) {
  $scope.services = data;
});