如何将数组存储到 $localStorage 中?

how to store the array into $localStorage?

我正在将两个值存储到一个数组中。他们是年龄和名字。如何使用 $localStorage 来存储这个数组?当我刷新页面时,array 和 $localStorage 都是空的。

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

这应该是您的控制器代码

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