javascript - angularjs and localStorage change event -
i store data in localstorage
what want in angularjs app when data in localstorage changed, app rerender app, how can this?
there angular localstorage module:
https://github.com/grevory/angular-local-storage
var democtrl = function($scope, localstorageservice) { localstorageservice.clearall(); $scope.$watch('localstoragedemo', function(value){ localstorageservice.add('localstoragedemo',value); $scope.localstoragedemovalue = localstorageservice.get('localstoragedemo'); }); $scope.storagetype = 'local storage'; if (!localstorageservice.issupported()) { $scope.storagetype = 'cookie'; } };
after further thought may need change module broadcast on setitem can notified if localstorage has been changed. maybe fork , around line 50:
localstorage.setitem(prefix+key, value); $rootscope.$broadcast('localstoragemodule.notification.setitem',{key: prefix+key, newvalue: value}); // broadcast old value if want
or in recent version of library casing changed
$rootscope.$broadcast('localstoragemodule.notification.setitem',{key: prefix+key, newvalue: value});
then in controller can:
$scope.$on('localstoragemodule.notification.setitem', function(event, parameters) { parameters.key; // contains key changed parameters.newvalue; // contains new value });
here demo of 2nd option: demo: http://beta.plnkr.co/lpam6szdm2orbm4loii1
** updated **
i forked project , have included notifications here in event want use project: https://github.com/sbosell/angular-local-storage/blob/master/localstoragemodule.js
i believe original library accepted pr. reason library has cookie backup in case browser doesn't support local storage.
Comments
Post a Comment