In this plunk I have a factory that wraps Angular's $cacheFactory
. The controller uses the factory to create two independent caches, however I get the following error: cacheManager is not a function
. What's wrong with this code?
HTML
<body ng-app="app" ng-controller="myCtl">
cache1 : {{cache1 | json}}
<br/>
cache2 : {{cache2 | json}}
</body>
Javascript:
var app = angular.module('app',[]);
app.controller('myCtl', function ($scope,cacheManager) {
var cache1 = new cacheManager();
var cache2 = new cacheManager();
cache1.create("cache1");
cache2.create("cache2");
cache1.add("key1", "value1");
cache2.add("key2", "value2");
})
.factory('cacheManager',function($cacheFactory){
var factory = {};
var cache = null;
factory.create = function(cacheId){
cache = $cacheFactory(cacheId);
return cache;
};
factory.add = function(key,value){
if (!cache)
return;
cache.put(key, value);
}
factory.get = function(key){
if (!cache)
return;
return cache.get(key);
}
return factory;
});