Friday 15 January 2010

Caching JSON with AngularJS -



Caching JSON with AngularJS -

this question has reply here:

angularjs : initialize service asynchronous data 8 answers

i'm loading json in controller, changing routes json gets reloaded. cache it.

var sampleapp = angular.module('sampleapp', [ 'ngroute', 'sampleappcontrollers', ]); var samplecontrollers = angular.module('samplecontrollers', []); samplecontrollers.controller('postsctrl', ['$scope', '$http', function($scope, $http) { // should loaded on app load $http.get('http://example.org/source.json').success(function(data) { $scope.posts = data; }); $scope.orderprop = 'id'; }]);

i tried using .constant, couldn't work:

sampleapp.constant('mycache', ['$http', function($http) { $http.get('http://example.org/source.json').success(function(data) { homecoming data; }); }]); samplecontrollers.controller('postsctrl', ['$scope', 'mycache', function($scope, mycache) { $scope.posts = mycache; $scope.orderprop = 'id'; }]);

i'm looking way load json on app start, , utilize in controllers.

that's angular services - load info within service instead of within controller, , have controller access data. services instantiated 1 time in lifetime of app, want here.

json angularjs

No comments:

Post a Comment