To reassure all parties, why not just use $ cacheFactory. This allows the data request services to be stateless and basically just the recipient and installer. I admit that storing data on $ rootScope or as a property in the service is convenient, but just feels wrong. Using $ cacheFactory is pretty simple.
First create a cache service:
angular.module('CacheService', ['ng']) .factory('CacheService', function($cacheFactory) { return $cacheFactory('CacheService'); });
Include the js file in your app.js and then enter it in the application declaration:
var MyApp = angular.module('MyApp', ['CacheService']);
Add it to the service, use it like this:
'use strict' MyApp.factory('HackerNewsService', function(CacheService) { return { getNews: function(key) { var news = CacheService.get(key); if(news) { return news; } return null; }, setNews: function(key, value) { CacheService.put(key, value); }, clearNews: function(key) { CacheService.put(key, ''); } }; });
Now all you have to do is enter the HackerNewsService into your controller and use it by calling the methods we created on it. For example:
HackerNewsService.setNews('myArticle', {headline: 'My Article', body: 'This is the body'}); $scope.article = HackerNewsService.getNews('myArticle');
Jordan Papaleo Oct 01 '13 at 15:10 2013-10-01 15:10
source share