1

我将两个值存储到一个数组中。他们是年龄和名字。如何使用 $localStorage 来存储这个数组?当我刷新页面时,数组和 $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

4

1 回答 1

1

这应该是您的控制器代码

$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={}
  }
于 2016-06-06T09:35:06.030 回答