<div ng-app="exampleStore">
<div ng-controller="MainCtrl">
<p>{{test}}</p>
<label>Local Storage Bound:</label> <input type="text" ng-model="test">
<a ng-click="clearTest()">Clear Value from LC</a>
<p><i>Change the value in the textbox and refresh the page!</i></p>
</div>
</div>
CSS
</style> <!-- Ugly Hack due to jsFiddle issue: http://goo.gl/BUfGZ --> <link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css"> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script> <style>
JavaScript
var eS = angular.module('exampleStore', ['localStorage']);
function MainCtrl($scope, $store) {
$store.bind($scope, 'test', 'Some Default Text');
$scope.clearTest = function(){
$store.remove('test');
};
}
var ls = angular.module('localStorage',[]);
ls.factory("$store",function($parse){
/**
* Global Vars
*/
var storage = (typeof window.localStorage === 'undefined') ? undefined : window.localStorage,
supported = !(typeof storage == 'undefined' || typeof window.JSON == 'undefined');
var privateMethods = {
/**
* Pass any type of a string from the localStorage to be parsed so it returns a usable version (like an Object)
* @param res - a string that will be parsed for type
* @returns {*} - whatever the real type of stored value was
*/
parseValue: function(res) {
var val;
try {
val = JSON.parse(res);
if (typeof val == 'undefined'){
val = res;
}
if (val == 'true'){
val = true;
}
if (val == 'false'){
val = false;
}
if (parseFloat(val) == val && !angular.isObject(val) ){
val = parseFloat(val);
}
} catch(e){
val = res;
}
return val;
}
};
var publicMethods = {
/**
* Set - let's you set a new localStorage key pair set
* @param key - a string that will be used as the accessor for the pair
* @param value - the value of the localStorage item
* @returns {*} - will return whatever it is you've stored in the local storage
*/
set: function(key,value){
if (!supported){
try {
$.cookie(key, value);
return value;
} catch(e){
console.log('Local Storage not supported, make sure you have the $.cookie supported.');
}
}
var saver = JSON.stringify(value);
storage.setItem(key, saver);
return privateMethods.parseValue(saver);
},
/**
* Get - let's you get the value of any pair you've stored
* @param key - the string that you set as accessor for the pair
* @returns {*} -...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.