如何存储数据到本地存储? [英] How to store the data to local storage?

查看:172
本文介绍了如何存储数据到本地存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我有将执行一些操作,例如从服务器检索数据,以及存储在服务器上它的自我服务。相反的,我想通过它转移到本地存储做的一切行动。

Actually i am having the service which will perform some action for example retrieve the data from server and as well as storing on server it self. Instead of that i want to do all that actions by diverting it to local storage.

推荐答案

这是一个有点我的code的存储和检索到本地存储。我用广播事件在模型中保存和恢复值。

this is a bit of my code that stores and retrieves to local storage. i use broadcast events to save and restore the values in the model.

app.factory('userService', ['$rootScope', function ($rootScope) {

    var service = {

        model: {
            name: '',
            email: ''
        },

        SaveState: function () {
            sessionStorage.userService = angular.toJson(service.model);
        },

        RestoreState: function () {
            service.model = angular.fromJson(sessionStorage.userService);
        }
    }

    $rootScope.$on("savestate", service.SaveState);
    $rootScope.$on("restorestate", service.RestoreState);

    return service;
}]);

这篇关于如何存储数据到本地存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆