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

时间:2015-01-23 19:56:58

标签: javascript json angularjs local-storage

我有这个表格

<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

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分:0)

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

这是相当简单的,但与其他人相比,它的运作非常可靠。

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

相关问题