Skip to content

Instantly share code, notes, and snippets.

@AlphaNerd
Created April 16, 2016 22:38
Show Gist options
  • Save AlphaNerd/00c9b205f5e526e79452aaee6c119c0d to your computer and use it in GitHub Desktop.
Save AlphaNerd/00c9b205f5e526e79452aaee6c119c0d to your computer and use it in GitHub Desktop.
Simple $localstorage service
angular.module('starter.services', [])
.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] || 'null');
},
clear: function() {
return $window.localStorage.clear();
}
}
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment