ngStorage 阵列在加载时重置
ngStorage Array Gets Reset on Load
我正在使用 ngStorage 并且我声明了一个我要将对象推送到的新数组。但是,每当页面重新加载时,数组都会重置。
有没有办法重写这个,这样我仍然可以声明新的空数组,然后将对象推送到本地存储中的数组,而不会在每次页面加载时都重置它?
var myFaves = [];
$scope.myFaves = myFaves;
$localStorage.myFaves = myFaves;
$scope.addFave = function(object){
$localStorage.myFaves.push(object);
console.log("$localStorage.myFaves", $localStorage.myFaves);
}
感谢帮助!
您可以在将其设置为空数组之前检查该数组是否存在。这样你就不会覆盖任何以前的值。
if ($localStorage.myFaves === undefined) {
$localStorage.myFaves = [];
}
我正在使用 ngStorage 并且我声明了一个我要将对象推送到的新数组。但是,每当页面重新加载时,数组都会重置。
有没有办法重写这个,这样我仍然可以声明新的空数组,然后将对象推送到本地存储中的数组,而不会在每次页面加载时都重置它?
var myFaves = [];
$scope.myFaves = myFaves;
$localStorage.myFaves = myFaves;
$scope.addFave = function(object){
$localStorage.myFaves.push(object);
console.log("$localStorage.myFaves", $localStorage.myFaves);
}
感谢帮助!
您可以在将其设置为空数组之前检查该数组是否存在。这样你就不会覆盖任何以前的值。
if ($localStorage.myFaves === undefined) {
$localStorage.myFaves = [];
}