Skip to content

Instantly share code, notes, and snippets.

@ivillamil
Last active January 21, 2016 11:50
Show Gist options
  • Save ivillamil/35b7b196142b63a9b34b to your computer and use it in GitHub Desktop.
Save ivillamil/35b7b196142b63a9b34b to your computer and use it in GitHub Desktop.
localStorage service wrapper for angularJS
(function () {
'use strict';
angular.module('app')
.factory('Storage', [function () {
var fn = {};
fn.has = function (key) {
var value = window.localStorage.getItem(key);
return !!value;
}
fn.get = function (key) {
if (!fn.has(key)) return false;
return JSON.parse(window.localStorage.getItem(key));
}
fn.set = function (key, value) {
var v = (typeof value === 'object') ? JSON.stringify(value) : value;
window.localStorage.setItem(key, v);
}
return fn;
}]);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment