Last active
January 21, 2016 11:50
-
-
Save ivillamil/35b7b196142b63a9b34b to your computer and use it in GitHub Desktop.
localStorage service wrapper for angularJS
This file contains 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 () { | |
'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