aahung aahung - 1 year ago 107
Javascript Question

AngularJS, save resource service json to localstorage and update conditionally

I need to update server-sided data(large) only when new version detected, I am using ngResource,

goodServices.factory('Good', ['$resource',
return $resource('../WebService/list_goods.ashx?gid=:gid', {}, {
query: {method:'GET', params:{q:'all'}, isArray:true}

how to save it to LocalStorage and every time detect server (GET a version number), when new version detected, it will refresh LocalStorage.

Answer Source

for communicating with local storage I want to suggest ngStorage you can inject it in your controller

function yourBaseController(Good,$localStorage){
   var newVersion = Good.get(..your params), // it should return your new version
       currentVersion = $localStorage.version;
   if(newVersion !== currentVersion ){
      $localStorage.version = newVersion;

Or if you don't have a base controller which is working at all time when your app bootstraps, you can move this logic in .run block

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download