Unable to access properties of JSON code through angularJS service -
i trying access properties of following json structure through angularjs factory , service methods:
{ "@context": { "firstname": "http://xmlns.com/foaf/0.1/firstname", "lastname": "http://xmlns.com/foaf/0.1/lastname" }, "members":[ { "firstname":"debjit", "lastname":"kumar" }, { "firstname":"hari", "lastname":"haran" }, { "firstname":"kaushik", "lastname":"shrestha" } ] }
but not able retrieve properties of retrieved json object. following angularjs code:
angular.module('rdfa',['ngresource']).config(['$routeprovider', function ($routeprovider) { $routeprovider.when('/', {controller: rdfactrl}); }]) .factory('members', function($resource){ return $resource('json.json', {}, { query: {method:'get', isarray:false} } ); }) .service('jsonservice',function(members){ this.members=members.query(); }); function rdfactrl($scope,jsonservice){ $scope.members=jsonservice.members.members; console.log(jsonservice.members); // outputs correct json structure console.log(jsonservice.members.members); // outputs undefined console.log(jsonservice.members['@context']); // outputs undefined }
you making asynchronous http request, cannot print output on same call cycle. in order need add success callback handler in query
action, example:
members.query(function(value) { console.log(value); // resource object console.log(value.members); // members object console.log(value['@context']); // @context object });
check plunker working example: http://plnkr.co/edit/tflq6cdkia3rpjkvhtoa?p=preview
Comments
Post a Comment