WebStorage Service for AngularJS
The webStorage service has both a generic and direct API. The generic API will check for client support and preferred order before altering a specific storage value, trying to degrade gracefully according to a set heuristic. The direct APIs works with either the client's local, session or the module's own in-memory storage engines.
The selection heuristics for the generic API is mainly dictated by a set order (defaults to ['local', 'session', 'memory'].) If the client has no support for the specified storage engine then the service will try to fall back on the next specified engine and so forth.
NOTE: The in-memory storage should really be seen as a last resort since all its values will be lost on page reload (somewhat negating the whole idea of client web storage!)
If the client does not support local or session web storage the module will try to mimic them by setting cookies on the current document.
All errors will be broadcast via the
$rootScope under a specific name (defaults to:
The service provides the following generic methods:
isSupported-- boolean flag indicating client support status (local or session storage)
add(key, value, all)-- add a value to storage under the specific key (storage according to 'order')
get(key, all)-- return the specified value (storage according to 'order')
remove(key, all)-- remove a key/value pair from storage (storage according to 'order')
clear(all)-- remove all key/value pairs from storage (storage according to 'order')
errorName(str)-- get or set the name of the event that is broadcast over the $rootScope on errors
prefix(str)-- get or set the prefix used for keys while operating on storage values
order(array)-- get or set the order by which storage models are iterated
It also provides the following direct APIs:
isSupported-- boolean flag indicating client support status (local storage)
add(key, value)-- add a value to storage under the specific key (local storage)
get(key)-- return the specified value (local storage)
remove(key)-- remove a key/value pair from storage (local storage)
clear()-- remove all key/value pairs from storage (local storage)
isSupported-- boolean flag indicating client support status (session storage)
add(key, value)-- add a value to storage under the specific key (session storage)
get(key)-- return the specified value (session storage)
remove(key)-- remove a key/value pair from storage (session storage)
clear()-- remove all key/value pairs from storage (session storage)
isSupported-- boolean true, the in-memory storage is always supported
add(key, value)-- add a value to storage under the specific key (in-memory storage)
get(key)-- return the specified value (in-memory storage)
remove(key)-- remove a key/value pair from storage (in-memory storage)
clear()-- remove all key/value pairs from storage (in-memory storage)
Fredric Rylander, https://github.com/fredricrylander/angular-webstorage
This module was built for AngularJS v1.0.5.
webStorageModule to your app's dependencies. Then inject
webStorage into any controller that needs to use it, e.g.:
- Paulo Cesar (https://github.com/pocesar)
- David Chang (https://github.com/hasdavidc)
- David Rodriguez (https://github.com/programmerdave)
- Initial commit.
- Bugfix: removed trailing commas that IE choked on, as reported by Paulo Cesar (pocesar).
- Now using the identity operator instead of equality when comparing prefixes while clearing storage.
- Bugfix: clearSession() is now actually clearing sessionStorage and not localStorage, as reported by David Chang (hasdavidc).
- Bugfix: now using the
errorNameconstant when broadcasting errors over the
- Bugfix: now using the
- Added strict mode.
- Bugfix: the module threw access denied exceptions under 'Protected Mode' in IE, as reported by (jswxwxf). Fixed by wrapping the sessionStorage and localStorage polyfillers in a try/catch-block.
getFromSession()will now return
nullon errors as expected, reported by David Rodriguez (programmerdave).
allEnginesas an argument to the generic methods (
clear). This enables the caller to decide if all supported storage engines should be queried or only the first supported one. The default value for this argument was chosen so that
add()will only use the first supported storage engine, while
clear()will query all supported storage engines. The update was inspired by David Rodriguez’s (programmerdave) pull request.
setStorageOrder()so that users of the module may alter the order by which storage models are iterated.
setStoragePrefix()so that users of the module may alter the prefix used when setting, getting or removing values from the store.
setErrorName()so that users of the module may alter the name of the event that is broadcast over the
$rootScopeon module errors.
- Updated the API documentation with
- Added the list of contributors to README.md.
- Added this changelog to README.md.
- Updated the API documentation with
setStoragePrefix()from being only setters into also being getters. To reflect this, they have been renamed
The MIT License Copyright (c) 2013 Fredric Rylander
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.