我正在使用 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 = [];
}
关于arrays - ngStorage 数组在加载时重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31778067/