jeudi 12 février 2015

angularJS force factories to reload dependencies


I have a factory that provides a value from local storage:



angular.module('myApp', [])
.factory('myFactory', function($localStorage){
return $localStorage.someValue;
});


sometimes the value in local storage is updated via another factory.


There's a third factory that uses the value from myFactory as a dependency:



angular.module('myApp')
.factory('doStuffWithStorage', function(myFactory, $http){
var doStuffWithStorage = {
getIt: function(){
return $http.get(myFactory) // myFactory is used here
}
}
return doStuffWithStorage
});


When the value in myFactory is changed, doStuffWithStorage does not use the updated value. I'm assuming because dependencies are loaded when angular boots and not dynamically updated?


I have to hard-refresh the site to get the new value loaded. Is there a way to make doStuffWithStorage always use the current value from the myFactory?





Aucun commentaire:

Enregistrer un commentaire