Security News
Namecheap Takes Down Polyfill.io Service Following Supply Chain Attack
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
ng-storage
Advanced tools
Readme
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.
bower install ngstorage
angular.module('app', [
'ngStorage'
]).controller('Ctrl', function(
$scope,
$localStorage,
$sessionStorage
){});
Pass $localStorage
(or $sessionStorage
) by reference to a hook under $scope
in plain ol' JavaScript:
$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 default values using the
$default()
method:
$scope.$storage = $localStorage.$default({ counter: 42 });
With this setup, changes will be automatically sync'd between $scope.$storage
, $localStorage
, and localStorage - even across different browser tabs!
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 can be easily seen by comparing the demos, this approach is way more verbose, and may have potential performance implications as the values being watched quickly grow.
Plain ol' JavaScript again, what else could you better expect?
// Both will do
delete $scope.$storage.counter;
delete $localStorage.counter;
This will delete the corresponding entry inside the Web Storage.
If you wish to clear the Storage in one go, use the $reset()
method:
$localStorage.$reset();
Optionally, pass in an object you'd like the Storage to reset to:
$localStorage.$reset({ counter: 42 });
You can store anything except those not supported by JSON:
Infinity
, NaN
- Will be replaced with null
.undefined
, Function - Will be removed.Just run $ npm install
to install dependencies. Then run $ grunt
for minification.
Any contribution will be appreciated.
FAQs
localStorage and sessionStorage done right for AngularJS.
The npm package ng-storage receives a total of 199 weekly downloads. As such, ng-storage popularity was classified as not popular.
We found that ng-storage demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.
Security News
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.