Reputation: 159
I'm using ngStorage and I declare a new array that I'm going to push objects to. However, whenever the page reloads the array gets reset.
Is there a way to re-write this so that I can still declare the new empty array and then push objects to the array in Local Storage without it being reset everytime the page loads?
var myFaves = [];
$scope.myFaves = myFaves;
$localStorage.myFaves = myFaves;
$scope.addFave = function(object){
$localStorage.myFaves.push(object);
console.log("$localStorage.myFaves", $localStorage.myFaves);
}
Appreciate the help!
Upvotes: 2
Views: 633
Reputation: 18193
You can check to see if the array exists before you set it to an empty array. That way you won't overwrite any previous values.
if ($localStorage.myFaves === undefined) {
$localStorage.myFaves = [];
}
Upvotes: 3