I'm having this little app, requesting the json file which works fine when using one level json, below code that works:
service:
angular
.module ('myApp')
.factory('Summary', function ($resource) {
return $resource('summary.json');
});
controller:
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}
]
output in console:
[Resource, Resource, Resource, Resource, Resource, Resource, $promise: Object, $resolved: true]
All that above works fine as I'm getting 6x Resource, as soon as I introduce multilevel json and amend the controller it is not working properly, below is new code where I'm trying to retrieve uk
:
new 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}
]
}
]
controller:
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);
});
}]);
output in console is undefined
What am I doing wrong here? Many thanks for your help.
data
is an array. You would have to dodata[0].uk
– KJ Price Aug 6 '14 at 13:21{"uk":[]}
instead of[{"uk":[]}]
– KJ Price Aug 6 '14 at 13:27{"uk":[]},{"us":[]}
but my IDE argue that there is an error which is comma in between them :( – angular_learner Aug 6 '14 at 13:37