An Angular service for client-side set, get, remove, bind, and clean of local storage mechanisms. It provides fallbacks to cookies or in-memory storage based on client capabilities and service configuration.
##Dependencies
- AngularJS - http://angularjs.org
- Angular Cookies - ngCookies - Only required if
cookieFallback
is enabled.
##Features
- Directly store Objects, Arrays, Floats, Booleans, and Strings. No need to convert objects to strings and then reverse them.
- Two way bind a $scope variable to localStorage or sessionStorage which will be updated whenever the model is updated, and vice versa.
- Cookie fallback if Storage is not supported.
- In-memory fallback if Storage is not supported and storing to cookies is disabled.
##Basic Setup
- Add this module to your app as a dependency:
var app = angular.module('yourApp', ['local-storage.service']);
- Inject $store as a parameter in declarations that require it:
app.controller('yourController', function($scope, $store){ ... });
##Configuration Options
The default configuration is:
- cookieFallback: true - if true, then session cookies are used for storage when the browser does not support the Storage interface. If false, then cookies are never used.
- useSessionStorage: false - if true, then sessionStorage is used instead of localStorage. If false, localStorage is used as the default. This is, of course, dependent on the browser supporting the Storage interface.
To override the default configuration options, configure the module with an options argument during application configuration:
app.config(['$storeProvider', function ($storeProvider) {
$storeProvider.configure({
cookieFallback: false,
useSessionStorage: true
});
}]);
##Basic Usage
###Binding
$store.bind($scope, 'viewType', 'cardView');
$scope.viewType = "ANYTHING";
###Unbinding
$store.unbind($scope, 'viewType');
###Set
$store.set("key", "value");
###Get
$store.get("key");
###Has
$store.has("key");
###Remove
$store.remove("key");
###Clear
$store.clear();
##Additional Methods
These methods were primarily implemented for testing purposes, but they may be useful in special scenarios and are part of the exposed API.
$store.getConfiguration();
$store.getSupported();
$store.setSupported(false);
$store.getStorage();
$store.getMemStore();
##Development
After forking you should only have to run npm install
from a command line to get your environment setup.
After install you have two gulp commands available to you:
gulp js:lint
gulp js:test