An AngularJS module that makes Web Storage working in the Angular Way. Contains two services: $localStorage
and $sessionStorage
.
-
No Getter 'n' Setter Bullshit - Right from AngularJS homepage: "Unlike other frameworks, there is no need to [...] wrap the model in accessors methods. Just plain old JavaScript here." Now you can enjoy the same benefit while achieving data persistence with Web Storage.
-
sessionStorage - We got this often-overlooked buddy covered.
-
Cleanly-Authored Code - Written in the Angular Way, well-structured with testability in mind.
-
No Cookie Fallback - With Web Storage being readily available in all the browsers AngularJS officially supports, such fallback is largely redundant.
angular.module('app', [
'ngStorage'
]).controller('Ctrl', function(
$scope,
$localStorage,
$sessionStorage
){});
Read and Write | Demo
Pass $localStorage
(or $sessionStorage
) by reference to a hook under $scope
:
$scope.$storage = $localStorage;
And use it like you-already-know:
<body ng-controller="Ctrl">
<button ng-click="$storage.counter = $storage.counter + 1">{{$storage.counter}}</button>
</body>
Optionally, specify a default value in plain ol' JavaScript:
$scope.$storage.counter = $localStorage.counter || 42;
With this setup, changes will be automatically sync'd between $scope.$storage
, $localStorage
, and localStorage.
Read and Write Alternative (Not Recommended) | Demo
If you're not fond of the presence of $scope.$storage
, you can always use watchers:
$scope.counter = $localStorage.counter || 42;
$scope.$watch('counter', function() {
$localStorage.counter = $scope.counter;
});
$scope.$watch(function() {
return angular.toJson($localStorage);
}, function() {
$scope.counter = $localStorage.counter;
});
This, however, is not the way ngStorage is designed to be used with. As you can easily see by comparing the demos, this approach is clearly way more verbose, and may have potential performance implications as the values being watched quickly grow.
Delete | Demo
Plain ol' JavaScript again, what else could you better expect?
// Both will work
delete $scope.$storage.counter;
delete $localStorage.counter;
This will delete the corresponding entry inside the Web Storage.
Delete Everything | Demo
Theoretically this can also be done in the plain ol' way but, we've got a convenient method just for you:
$localStorage.$clear();
- ngdoc Documentation
- Namespace Support
- Unit Tests
- Grunt Tasks
Any contribution will be appreciated.