Skip to content

Instantly share code, notes, and snippets.

@timofurrer
Created September 22, 2015 05:51
Show Gist options
  • Save timofurrer/af51a6a670f84a8b52d8 to your computer and use it in GitHub Desktop.
Save timofurrer/af51a6a670f84a8b52d8 to your computer and use it in GitHub Desktop.
AngularJS localStorage service
(function(module) {
var localStorage = function($window) {
var storage = $window.localStorage;
var set = function(key, value) {
storage.setItem(key, angular.toJson(value));
};
var get = function(key) {
var value = storage.getItem(key);
if(value) {
return angular.fromJson(value);
}
return value;
};
var remove = function(key) {
storage.removeItem(key);
};
return {
set: set,
get: get,
remove: remove
};
};
module.factory('localStorage', localStorage);
}(angular.module('common')));
@xeoncross
Copy link

Should really wrap in try/catch block because of possible exceptions: https://gist.github.com/drGrove/599fd38f509a439825b2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment