如何消除此错误(获取错误 $localStorage.getItem 不是函数)?

how to remove this error (getting error $localStorage.getItem is not a function)?

我正在尝试将我的数据保存在本地存储中,但出现错误 $localStorage.getItem is not a function.Actually I am using ionic framework 。我需要持久存储数据并从持久性中获取数据。我使用 ngstorage.js。我还包括该模块,但出现未定义错误

这一行出现错误 return $localStorage.getItem("list"); 这是我的代码 https://dl.dropbox.com/s/l2dtrxmnsurccxt/www.zip?dl=0

(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;
            }
        }

    }
})();

编辑

 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);
    }

对于 ngStorage 库,没有 getItem() 方法,您只需使用标准对象成员访问进行读写:

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

以上是我对上述问题的看法

  • 想想你错过了注入 'ngStorage' 模块。
  • 您可以使用 $localStorage.list.
  • 而不是 $localStorage.getItem("list")
  • 您可以使用 $localStorage.list = data
  • 而不是 $localStorage.setItem("list",JSON.stringify(data));

函数 getItem 用于 HTML5 localStorage。在您的代码中,您使用 AngularJS $localStorage.

所以你应该使用 $localStorage.get('list');

有关 de AngularJS $localStorage 的更多信息,您应该阅读此 -> http://ghost.scriptwerx.io/angularjs-localstorage/

更新 仅适用于 AngularJS $localStorage。问题是针对 ngstorage.js localStorage.