我有这个小应用程序,请求json文件在使用一个级别的json时工作正常,下面的代码可以工作:
服务:
angular
.module ('myApp')
.factory('Summary', function ($resource) {
return $resource('summary.json');
});
控制器:
angular
.module ('myApp')
.controller ('summaryCtrl', ['$scope', 'poller','Summary', function ($scope, poller,Summary) {
var poller1;
//using angular-poller
poller1 = poller.get(Summary, {delay: 2000});
poller1.promise.then(null, null, function (data) {
$scope.summary = data;
console.log(data);
});
}]);
JSON:
[
{"channel": "aaa", "value": 13256},
{"channel": "bbb", "value": 6598},
{"channel": "ccc", "value": 245 },
{"channel": "ddd", "value": 123},
{ "channel": "eee", "value": 956},
{ "channel": "fff", "value": 142}
]
控制台输出:
[Resource, Resource, Resource, Resource, Resource, Resource, $promise: Object, $resolved: true]
以上所有内容都可以正常工作,因为我获得了6x资源,只要我引入多级json并修改控制器它就无法正常工作,下面是新代码,我试图检索{{ 1}}:
new json:
uk
控制器:
[
{
"uk": [
{"channel": "aaa", "value": 13256},
{"channel": "bbb", "value": 6598},
{"channel": "ccc", "value": 245 },
{"channel": "ddd", "value": 123},
{ "channel": "eee", "value": 956},
{ "channel": "fff", "value": 142}
]
},
{
"us": [
{"channel": "aaa", "value": 457},
{"channel": "bbb", "value": 364},
{"channel": "ccc", "value": 457 },
{"channel": "ddd", "value": 45},
{ "channel": "eee", "value": 3},
{ "channel": "fff", "value": 143562}
]
}
]
控制台中的输出为angular
.module ('myApp')
.controller ('summaryCtrl', ['$scope', 'poller','Summary', function ($scope, poller,Summary) {
var poller1;
poller1 = poller.get(Summary, {delay: 2000});
poller1.promise.then(null, null, function (data) {
$scope.summary = data.uk; //here I need to get uk data
console.log(data.uk);
});
}]);
我在这里做错了什么?非常感谢你的帮助。
答案 0 :(得分:0)
将json的格式更改为:
{
"uk": [
{"channel": "aaa", "value": 13256},
{"channel": "bbb", "value": 6598},
{"channel": "ccc", "value": 245 },
{"channel": "ddd", "value": 123},
{ "channel": "eee", "value": 956},
{ "channel": "fff", "value": 142}
],
"us": [
{"channel": "aaa", "value": 457},
{"channel": "bbb", "value": 364},
{"channel": "ccc", "value": 457 },
{"channel": "ddd", "value": 45},
{ "channel": "eee", "value": 3},
{ "channel": "fff", "value": 143562}
]
}
然后data.uk和data.us将产生所需的结果。