0

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; }

1 Answer 1

1

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]); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Welcome! Please mark the answer correct if it works for you (by clicking on the check mark on my answer)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.