Reputation: 110
I have two controllers and a factory with some utility functions I use in both controllers. One of the utility functions checks the cache ($cacheFactory
) for some value, and I can't get it to work, no matter what.
app:
angular.module('sfmiet', ['sfmiet.autocomplete', 'sfmiet.httpservice', 'sfmiet.utilservice']);
factory:
angular.module('sfmiet.utilservice', [])
.factory('utilservice', ['$cacheFactory', function($cacheFactory) {
return function(cacheName) {
this.cache = $cacheFactory.get(cacheName);
this.init = function(cacheName) {
this.cache = $cacheFactory.get(cacheName);
};
this.isActiveStep = function(step) {
return (this.cache.get('step') == step);
};
};
}]);
controller 1:
angular.module('sfmiet').controller('sftestCtrl', function($scope, $cacheFactory, utilservice) {
$scope.util = new utilservice('stateCache');
console.log($scope.util);
//some more stuff...
});
controller 2:
angular.module('sfmiet').controller('sfmietCtrl', function($scope, $timeout, $http, $cacheFactory, autocomplete, httpservice, utilservice) {
$scope.util = new utilservice('stateCache');
console.log($scope.util);
//some more stuff...
});
logging $scope.util.cache
for the controllers gives me an object for the second but undefined for the first. Not using the new keyword leaves $scope.util
completely undefined for the first controller, while again beeing fine with the second. All works fine with only one controller, but as soon as I bring the second in, it stops working.
Maybe I'm missing something here, but I can't see the reason for this behavior. Been searching the web for the last 3 hours, and since I'm at the end of my rope now, I hope you people can give me a poke. Thanks very much!
Upvotes: 1
Views: 141
Reputation: 11372
According to the documentation, $cacheFactory.get
returns undefined
if the cache does not exist.
The most likely cause is that stateCache
is created after your first controller, and before your second controller.
Upvotes: 1