angularjsng-storage

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


Solution

  • 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={}
      }