Search code examples
angularjslocalforage

Local Forage and Angular - issue with Get Item from Local Storage


I have an angular model named rows.

I have a piece of code that tries to fetch data from local storage using LocalForage and set the local data to the rows model.

Apparently, it seems like, by the time the local storage call back for get item executes, the $scope.rows model goes out of scope.

Is there a way, i can achieve this, that is set the value from local storage to the rows model?

    localforage.getItem(localBlogKey, function (err, readValue) 
    {
     if (readValue !== null && readValue !== undefined) 
         {
           var localdata = readValue.data;                    
           $scope.rows = angular.fromJson(localData);
         }
     });

Since this piece of code runs asynchronously, the $scope.rows goes out of scope? Is there a sunchronous way to pull data from localforage?


Solution

  • This is achieved using the ngStorage Module. Import the ngStorage module and then access the localstorage item with the key as below

      localstorage.myKey = "myValue";
    

    This is Synchronous and hence my issue is resolved.