我正在创建一个简单的注册 webapp,它将数据存储在本地存储元素中。我正在使用 ngStorage 框架。
这是我的js代码..
var app = angular.module("myApp", ['ngRoute', 'ngStorage']);
var count = 1;
var id;
app.controller('myCtrl1', ['$scope', '$location', function($scope,$localStorage) {
$scope.flag = false;
$scope.goUrl = function (hash) {
$location.path(hash);
};
if ($localStorage.array==undefined)
{
$localStorage.array = [];
}
$scope.submit = function () {
if($scope.flag == true)
{
$scope.edit();
console.log(id);
}
else
{
$scope._add();
}
};
$scope._add = function(){
$scope.donor.id = count;
++count;
$localStorage.array.push($scope.donor);
$scope.donor = {};
alert("Submitted");
};
$scope.array = $localStorage.array; //ctrl2
$scope.delete = function (id) {
--id;
if (id > -1) {
$localStorage.array.splice(id, 1);
}
$scope.array = $localStorage.array;
};
$scope.open = function (x){
$scope.flag = true;
id = x;
};
$scope.edit = function (){
$scope.donor.id = id;
$localStorage.array[--id] = $scope.donor;
$scope.array = $localStorage.array;
$scope.donor = {};
$scope.flag = false;
};
}]);
当我重新加载页面或关闭页面时,localstorage 中的数据会丢失。我检查了开发工具。我可以看到没有任何内容写入本地存储,即使该应用程序在当前会话中运行良好。我做错了什么吗?如果您需要更多信息,请告诉我..