AngularJs 如何过滤 $cacheFactory

AngularJs How to filter $cacheFactory

我有一个 $cacheFactory

 /* @ngInject */
MenuSvcCache.$inject = ['$cacheFactory'];

app.factory('MenuSvcCache', MenuSvcCache);
function MenuSvcCache($cacheFactory){
    return $cacheFactory('MenuSvc')
};

在我的LayoutCtrl中使用:

    function Init(router) {
    if (MenuSvcCache) {
        layout.menuItems = MenuSvcCache.get('MenuSvc');            
    } else {
        MenuSvc.all().success(function (data) {
            MenuSvcCache.put('MenuSvc', data)
            layout.menuItems = MenuSvcCache.get('MenuSvc');
        })
            .error(function (error) {
                console.log('Error: ' + error.message)
            });
    }
}

我可以随意过滤那个缓存吗?像...

        if(MenuSvcCache && router){   
        layout.menuItems = MenuSvcCache.get('MenuSvc').$filter(router);   
        } else {
        layout.menuItems = MenuSvcCache.get('MenuSvc'); 
        }     

我意识到“.$filter(router)”的语法不正确,但是有什么我可以使用的语法吗?

您需要手动跟踪密钥 - 请参阅此插件

http://plnkr.co/edit/vQfo0KAHPxs6K9t437i1?p=preview

(function(angular) {
  'use strict';
angular.module('cacheExampleApp', []).
  controller('CacheController', ['$filter', '$scope', '$cacheFactory',
  function($filter, $scope, $cacheFactory) {
    $scope.keys = [];
    $scope.cache = $cacheFactory('cacheId');
    $scope.put = function(key, value) {
      if ($scope.cache.get(key) === undefined) {
        $scope.keys.push(key);
      }
      $scope.cache.put(key, value === undefined ? null : value);
    };

    $scope.put('apples', {data: 1});
    $scope.put('pears', {data: 2});
    $scope.put('oranges', {data: 3});
    $scope.put('pineapples', {data: 1});
    $scope.put('cherries', {data: 1});

  }]);
})(window.angular);

angular.module('cacheExampleApp').filter('onesOnly', function() {
  return function(cacheEntry) {
      return cacheEntry.data === 1;
  }
})