angular.js - 在块之间共享cacheFactory数据

时间:2016-10-10 09:07:04

标签: angularjs caching

我想在$cacheFactory块和服务之间共享相同的.run。 我在$cacheFactory块中定义了.run

var cache = $cacheFactory('cacheName');

并把一些东西放进去:

cache.put('appData', data);

然后我想检查我的某个服务中的密钥是否存在,但我得到的错误是 - CacheId 'CacheName' is already taken!

这是服务代码:

$scope.cache = $cacheFactory('cacheName');
if (angular.isUndefined($scope.cache.get(key))) {
   $log.log($scope.cache.get(key)); //printing th key
}

那我该如何共享缓存数据?

2 个答案:

答案 0 :(得分:2)

很难说没有看到你的其余代码。但我的下面的例子可以使用

(function(angular) {
  'use strict';

  angular
    .module('cacheExampleApp', [])
    .controller('CacheController', ['$scope', '$cacheFactory', 'notify', function($scope, $cacheFactory, notify) {

      $scope.cache = $cacheFactory('cacheName');
      $scope.cache.put('appData', 'some data');

      $scope.callNotify = function() {
        notify();
      };

    }])
    .factory('notify', ['$window', '$cacheFactory', function(win, $cacheFactory) {

      return function() {
        var dataCache = $cacheFactory.get('cacheName');
        win.alert(dataCache.get('appData'));
      };

    }]);

})(window.angular);

示例:https://plnkr.co/edit/axiPg5qzxLTyGLy8pMu1?p=preview

答案 1 :(得分:0)

所以我用service来存储数据,服务代码:

(function () {
    'use strict';
    var cacheService = function ($log, $cacheFactory) {
        //define cache factory 
        var cache = $cacheFactory('cacheID');
        /*put key-val in cache*/
        var _putData = function(key, val){
            //check for undefined 
            if(!angular.isUndefined(key) && !angular.isUndefined(val)){
                //key must be a String
                if(angular.isString(key)){
                    cache.put(key, val);
                }else{
                    $log.error('Error in cacheService key must be a String type only!');    
                }

            }else{
                $log.error('Error in cacheService key or value are undefined');
            }
        };
        /*get data from cache */
        var _getData = function(key){
            //check for String type
            if(angular.isString(key)){
               return cache.get(key);
            }else{
                $log.error('Error in cacheService key must ne a String type');
            }
        };
        /*Get info */
        var _info = function(){
            return cache.info();
        };
        /*Remove data */
        var _remove = function(key){
            //check for String type
            if(angular.isString(key)){
                cache.remove(key);
            }else{
                $log.error('Error in cacheService key must ne a String type');
            }
        };
        /*clear cache*/
        var _removeAll = function(){
            cache.removeAll();
        };
        /*  PUBLIC API  */
        var service = {
            //model: _model,
            put : _putData,
            get : _getData,
            remove : _remove,
            removeAll : _removeAll
        };

        return service;

    };


    angular.module('common').service('cacheService', cacheService);


})();

然后我只需将它注入任何需要的地方。