How can I monitor changes to localStorage using $watch?
I have created a factory to simplify setting and getting values
.factory('$localstorage', ['$window', function($window) {
return {
set: function(key, value) {
$window.localStorage[key] = value;
},
get: function(key, defaultValue) {
return $window.localStorage[key] || defaultValue;
},
setObject: function(key, value) {
$window.localStorage[key] = JSON.stringify(value);
},
getObject: function(key) {
return JSON.parse($window.localStorage[key] || '{}');
}
}
}]);
In my controller, I am utilizing the factory like this:
.controller('CodesCtrl', function($scope, $localstorage) {
$scope.codes = $localstorage.getObject('codes');
...
In another controller, data is being added to localStorage. I want to update the view as soon as there are changes in localStorage.
I have come across suggestions to use ngStorage, but I prefer avoiding that if possible.
Is it feasible to achieve this using $watch? If so, could someone guide me in the right direction?