使用 angularjs 将数据保存在 json 本地存储中

save data in json local storage with angularjs

我有这个表格

<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/agrublev/angularLocalStorage

它非常简单,但与其他相比它工作起来非常可靠。

你可以将你的 ng-model 直接绑定到本地存储密钥然后就完成了,或者你可以在更适合的地方使用旧式 getter/setter。

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

localStorageService.set("model", $scope.model);

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

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