【问题标题】:Save results of Angular form in JSON locally [closed]在本地以 JSON 格式保存 Angular 表单的结果 [关闭]
【发布时间】:2013-08-13 00:43:23
【问题描述】:

我想制作一个表单(在 angularJS 中),提交时保存在本地,以后可以访问。我需要哪些代码来读取和写入这个 JSON 文件,或者我可以在哪里学习如何执行此操作?

【问题讨论】:

  • 看看 HTML5 本地存储。这将允许您在客户端上保存一个 json 字符串并稍后再次访问它。
  • 看起来像个骗子follow this link

标签: javascript html json angularjs


【解决方案1】:

我喜欢grevory的本地存储服务:https://github.com/grevory/angular-local-storage

你也可以看看 HTML5 web SQL:http://www.tutorialspoint.com/html5/html5_web_sql.htm 不过,不确定它是否支持。

至于字符串处理部分,Angular 有:http://docs.angularjs.org/api/angular.toJsonhttp://docs.angularjs.org/api/angular.fromJson

【讨论】:

  • +1 用于 angular-local-storage。它非常易于使用,并且可以按照您的预期工作。
【解决方案2】:

这是我的 Angular 服务中的一些代码。希望你能适应你的目的

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

    return service;
}]);

【讨论】:

    猜你喜欢
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 2022-01-24
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多