angular.js本地存储 - 初始化app检索本地存储的数据

时间:2014-07-20 09:13:25

标签: angularjs local-storage ionic-framework

我对角度很新,我试图避免在用户刷新页面时丢失在简单购物车应用程序上添加的项目。

我正在使用angularLocalStorage(https://github.com/agrublev/angularLocalStorage),但不知道如何检索内容。 我的台词:

var myApp = angular.module('ionicApp', ['ionic','angularLocalStorage']);
myApp.factory('prodottiData', function($http) {
  return {
    getFooOldSchool: function(callback) {
    $http.get('http://192.168.1.128/hongkongapp/?json=get_recent_posts&post_type=product&custom_fields=all').success(callback);
    }
  }
 });
myApp.factory('DataService', function() {
  var myCart = new shoppingCart("AngularStore");
  return {
    cart : myCart
  };
});
myApp.controller('MyController', function MyController ($scope, storage, $ionicSideMenuDelegate, prodottiData, DataService, $sce) {
    $scope.toggleLeft = function() {
    $ionicSideMenuDelegate.$getByHandle('mainMenu').toggleLeft();
  };
  $scope.toggleMySecondMenuLeft = function() {
    $ionicSideMenuDelegate.$getByHandle('mySecondMenu').toggleLeft();
  };
    //adding menu data to the scope object
    prodottiData.getFooOldSchool(function(data) {
        $scope.menu = data;
    });
    //adding the cart to the scope object
    $scope.cart = DataService.cart;
    $scope.to_trusted = function(html_code) {
        return $sce.trustAsHtml(html_code);
    }
    images = $scope.menu;
    $scope.showloader = function(){            
            $scope.shownImage = this.post.thumbnail_images.full.url;
            $scope.itemDesc = this.post.content;
            $scope.itemPrice = this.post.custom_fields._price[0];
            $scope.productName = this.post.title;
            $scope.skuProdotto = this.post.id;
        }
    });

现在,如果我在控制台上检查localstorage,我可以看到确实存储了一些内容,但我错过了在启动时重新填充购物车的方法。

任何帮助都会很棒!

3 个答案:

答案 0 :(得分:1)

为什么不使用浏览器本地存储? 您可以将它作为新服务添加到services.js中,然后使用它。

var storeService = myAppServices.factory('storeService', function() {

    var service =
    {
        setClientData:function(client_details)
        {
            window.localStorage.setItem( "client_data", JSON.stringify(client_details) );
            client_data = client_details;
        },
        getClientData:function()
        {
            if (client_data == null)
            {
                client_data = JSON.parse(window.localStorage.getItem("client_data"));
            }
            return client_data;
        }
    }

    var client_data = null;

    return service;
});

答案 1 :(得分:0)

从文档中检索,storage.get('key')

所以,刷新后检查:

if (storage.get('someKey')){
  $scope.retrieved_value = storage.get('someKey');
}else{
// whatever
}

答案 2 :(得分:0)

您可以使用localStorage而不是windows.localStorage。



 if(typeof(Storage)!=="undefined")
        {
            // Code for localStorage/sessionStorage.
            var hello = "Hello World!!";

            localStorage.setItem("hello",hello);
            // get string
            console.log(localStorage.getItem("hello")); // will return 'Hello World!!'


            var me = {name:'abel',age:26,gender:'male'};
            localStorage.setItem("user", JSON.stringify(me));
            //fetch object
            console.log(localStorage.getItem("user")); // will return {"name":"myname","age":99,"gender":"myGender"}
            var objetos = JSON.parse(localStorage.getItem("user"));
            console.log(objetos.name);

        }
        else
        {
            // Sorry! No Web Storage support..
        }