Reputation: 71
I want to add new items into $localStorage plus old items. But, here in my code $localStorage loosing all previous items. My code is as follows,
$scope.cart = [];
$scope.cart = productService.getSharedProduct();
if ($scope.cart != 0) {
$localStorage.items = $scope.cart;
}
else {
$scope.cart = $localStorage.items;
}
Upvotes: 0
Views: 89
Reputation: 9000
Yes because you are not adding items to your $localStorage.items
but you are assigning new values to it every time and so it lost the last added values.
You are re-initializing them every time and so they loose last added values.
You should do something like this
if ($scope.cart != 0) {
// instead of this
//$localStorage.items = $scope.cart;
// you should do this
for(var i=0; i<$scope.cart.length; i++)
{
$localStorage.items.push($scope.cart[i]);
}
}
Upvotes: 1