How can I remove this error (getting error $ localStorage.getItem is not a function)?

I am trying to save my data to local storage, but I am getting the error $ localStorage.getItem is not a function. I am actually using ionic framework. I need to store data persistently and get data from persistance. I am using ngstorage.js. I also include this module, but I get undefined error

error on this line       Here is my code https://dl.dropbox.com/s/l2dtrxmnsurccxt/www.zip?dl=0 return $localStorage.getItem("list");

(function(){
    'use strict';

    angular.module('app.stationselect').factory('stationListDownload', stationListDownload);
    stationListDownload.$inject=['$http','$localStorage']
    function stationListDownload($http,$localStorage){
        var list;
        return {
            getDownloadList: function(){
               return $http.get("http://caht.firstrail.com/FGRailApps/jservices/rest/stationList");
            },
            getlist :function(){
               return $localStorage.getItem("list");
            },
            setList :function (list){
                this.list=list;
            }
        }

    }
})();

      

change

 var list= stationListDownload.getlist();
    if(list==null ||typeof list=='undefined' ){
        stationListDownload.getDownloadList().then(function(data){
            $scope.loadingIndicator.hide();
            console.log("success")
            $localStorage.setItem("list",JSON.stringify(data));
        },function(error){
            console.log("error")
        })
    }else {
        console.log('else condition')
       cosole.log(list);
    }

      

+3


source to share


3 answers


This is my thought on the above question



  • Think you missed to introduce the 'ngStorage .
  • $localStorage.getItem("list")

    You can use instead $localStorage.list

    .
  • Instead, $localStorage.setItem("list",JSON.stringify(data));

    you can use$localStorage.list = data

+4


source


There is no method for the ngStorage library getItem()

, you just read and write using the standard object member access:



getlist :function(){
   return $localStorage.list;
},
setList :function (list){
    $localStorage.list=list;
}

      

+1


source


The getItem function is for HTML5 local storage. In your code, you are using AngularJS $ local storage.

So you have to use $ localStorage.get ('list');

For more details on de AngularJS $ localStorage you should read this -> http://ghost.scriptwerx.io/angularjs-localstorage/

Update For AngularJS only $localStorage

. The question is about local storage ngstorage.js

.

0


source







All Articles