Rubel Rubel - 4 months ago 19x
AngularJS Question

$localStorage is not adding new items with the old items

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;


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++)