我有一个$ 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(路由器)"语法不正确,但是我可以使用任何语法吗?
答案 0 :(得分:0)
您需要手动跟踪密钥 - 请参阅此plunkr
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;
}
})