使用 AngularJS 将数据保存在 JSON 本地存储中

save data in json local storage with angularjs

本文关键字:存储 JSON 存在 AngularJS 数据 保存 使用      更新时间:2023-09-26

我有这个表格

<form ng-submit="addState()">
            <input ng-model="text1" type="text">
            <input ng-model="text2" type="text">
            <input ng-model="text3" type="text">     
</form>

和这个 JS

mainAppControllers.controller('DoarScanCtrl', ['$scope', '$routeParams', '$location',
    function($scope, $routeParams, $location){
    }
]);

所以首先我需要将数据保存在本地存储中,然后我需要有一个 json

感谢您的任何帮助!

我会推荐这个库 https://github.com/grevory/angular-local-storage 它还可以自动将对象和数组转换为 json

localStorageService.set("model", $scope.model);
$scope.model2= localStorageService.get("model");

我做了一个小提琴如何使用 http://jsfiddle.net/jgs4280c/

我已经尝试了几个可用的本地存储包并确定了 https://github.com/agrublev/angularLocalStorage

非常简单,但与其他方法相比,它的工作非常可靠。

而且,您可以将ng-model直接绑定到本地存储密钥,然后完成,或者您可以在更适合的地方使用传统样式的getter/setter。