我遇到了使用以下结构从json文件中检索数据的麻烦。 在我的controller.js文件中,我有:
var controllers = angular.module('hotels.controllers', []);
controllers.controller('AppCtrl', function ($scope, $http){
$scope.list;
$http.get('json-folder/name.json').success(function(data, status, headers, config) {
$scope.list = data.data;
console.log(data);
});
在那个name.json文件中,我有以下结构:
{
"hotel": [
{
"id": 1,
"name": "Some hotel name",
"address": "123 Street",
"category" : [{
"id": 1,
"name" : "Category 1",
"bnb" : "yes",
"simple" : "some value",
"double" : "another value"
},
{
"id": 2,
"name" : "Category 2",
"bnb" : "no",
"simple" : "some value 2",
"double" : "another value 2"
},
{
"id": 3,
"name" : "Category 3",
"bnb" : "no",
"simple" : "some value 3",
"double" : "another value 3"
}
]
},
{
"id": 2,
"name": "Some hotel name 2",
"address": "33333 Street",
"category" : [{
"id": 1,
"name" : "Category 1",
"bnb" : "yes",
"simple" : "some value",
"double" : "another value"
},
{
"id": 2,
"name" : "Category 2",
"bnb" : "no",
"simple" : "some value 2",
"double" : "another value 2"
},
{
"id": 3,
"name" : "Category 3",
"bnb" : "no",
"simple" : "some value 3",
"double" : "another value 3"
}
]
}
],
"motel": [
{
"id": 1,
"name": "Some motel name",
"address": "321 Street",
"category" : [{
"id" : 1,
"name" : "Category Motel 1",
"bnb" : "yes",
"simple" : "some motel value",
"double" : "another motel value"
},
{
"id" : 2,
"name" : "Category Motel 2",
"bnb" : "no",
"simple" : "some motel value 2",
"double" : "another motel value 2"
}
]
}
]
}
最后是我的html结构:
<div ng-controller="AppCtrl">
<ul class="pull-left">
<li class="pull-left" ng-repeat="hotel in list.hotels">
{{hotel.name}}
</li>
</ul>
</div>
<div ng-controller="CatCtrl">
<ul class="pull-right">
<li class="pull-right" ng-repeat="cat in list.categories">
{{cat.name}}
</li>
</ul>
</div>
我没有得到任何回报,我没有任何错误...... 我想要的是一个带有酒店名称的ul li和另一个类似的div我希望再次列出类别名称以及酒店类别下的其他数据。请注意,CatCtrl与AppCtrl相同......
知道第一个我做错了什么,我应该在其他控制器中改变什么才能得到我需要的东西?
谢谢。
答案 0 :(得分:1)
现在您已经掌握了第一部分,获取类别应该要求将代码更改为:
<div ng-controller="AppCtrl">
<ul class="pull-left">
<li class="pull-left" ng-repeat="hotel in list.hotel">
{{hotel.name}}
<ul class="pull-right">
<li class="pull-right" ng-repeat="cat in hotel.category">
{{cat.name}}
</li>
</ul>
</li>
</ul>
</div>
我会依靠你的CSS技巧来正确安排显示。