Created
September 22, 2015 05:51
-
-
Save timofurrer/af51a6a670f84a8b52d8 to your computer and use it in GitHub Desktop.
AngularJS localStorage service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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'))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should really wrap in try/catch block because of possible exceptions: https://gist.github.com/drGrove/599fd38f509a439825b2