SrinivasAppQube
SrinivasAppQube

Reputation: 301

how to store the array into $localStorage?

I am storing two values into an array. They are age and name. How do I use $localStorage to store this array? When I refresh the page, both the array and $localStorage are null.

var app=angular.module("plunker",["ngStorage"]);
app.controller("xCtrl",function($scope,$rootScope,$localStorage){
   $scope.list=[]
   $scope.x={}
   $scope.add=function(x){
   $scope.list.push(x);
   $localStorage.list= $scope.list;
   $scope.$storage= $localStorage.list
   $scope.x={}
}
})

https://plnkr.co/edit/6vGduA4hWzcuYhA1fwHC?p=preview

Upvotes: 1

Views: 427

Answers (1)

Rakesh Chand
Rakesh Chand

Reputation: 3113

This should be your controller code

$scope.list=[]
   $scope.x={}
   if($localStorage.list){
     $scope.$storage=$localStorage.list;
     $scope.list=$localStorage.list;
   }
   $scope.add=function(x){
    $scope.list.push(x);
    $scope.$storage= $localStorage.list
    $scope.x={}
  }

Upvotes: 1

Related Questions