Angularjs 将数组存储到Cookie中

Angularjs 将数组存储到Cookie中,angularjs,Angularjs,我在下面有一个功能,可以很好地将信息传递到cookieStore并从中检索,没有问题 $scope.num = 0 $scope.nums = [] $scope.increment = function () { $scope.num++ } $scope.$watch('num', function () {

我在下面有一个功能,可以很好地将信息传递到cookieStore并从中检索,没有问题

            $scope.num = 0
            $scope.nums = []

            $scope.increment = function () {
                $scope.num++
            }
            $scope.$watch('num', function () {
                $scope.nums.push($scope.num)
            })
            $scope.storeProductsInCookie=function(){
                $cookieStore.put("invoices",$scope.nums)
            };
            $scope.getProductsInCookie=function(){
                console.log(  $cookieStore.get("invoices",$scope.nums));
            }
但是,当我尝试使用下面的verufyGuess函数时,该函数将索引和no作为参数注入函数中,它无法放入cookieStore中

$scope.verifyGuess=函数(索引,编号){


$cookieStore
已弃用:(自v1.4.0起) 请改用
$cookies
服务

$scope.nums
是字符串数组。因此
$cookieStore
将它们存储为字符串(Cookie仅支持存储字符串)

在第二种情况下,
$scope.newvows
是一个对象数组。Cookie不会存储对象

您必须使用
$cookieStore.put(“发票”,JSON.stringify($scope.newvows))


文件。写(“”);
var app=angular.module('plunker',['ngCookies']);
app.controller('MainCtrl',函数($scope,$cookies){
$scope.voces={};
$scope.newvows=[]
var指数=0;
$scope.voces[Index]=$scope.voces[Index]| 0;
$scope.vows[索引]+=10;
$scope.newvoces.push($scope.voces)
$scope.storeProductsInCookie=函数(){
$cookies.put(“发票”,JSON.stringify($scope.newvows))
};
$scope.getProductsInCookie=函数(){
console.log($cookies.get(“发票”));
}
$scope.storeProductsInCookie();
$scope.getProductsInCookie();
});

失败发生在这一行$cookieStore.put(“发票“,$cope.newvots)?请详细说明您的答案I add and get cookie它返回[{“9”:1}],然后我刷新页面,再次添加并获取cookie,它返回[{“10”:1}]我要的结果是[{“9”:1}{“10”:1}],并附加到以前的结果中
                $scope.votes = {};
                $scope.newVotes = []

                 $scope.votes[Index] = $scope.votes[Index] || 0;
                 $scope.votes[Index]+= no;

                $scope.$watch('votes', function () {
                    $scope.newVotes.push($scope.votes)
                })
                $scope.storeProductsInCookie=function(){
                    $cookieStore.put("invoices",$scope.newVotes)
                };
                $scope.getProductsInCookie=function(){
                    console.log(   $cookieStore.get("invoices",$scope.newVotes));
                }
 <!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <link rel="stylesheet" href="style.css" />
  <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.12/angular-cookies.js"></script>
  <script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
  <script>
    var app = angular.module('plunker', ['ngCookies']);

    app.controller('MainCtrl', function($scope, $cookies) {

      $scope.votes = {};
      $scope.newVotes = []
      var Index = 0;
      $scope.votes[Index] = $scope.votes[Index] || 0;
      $scope.votes[Index] += 10;


      $scope.newVotes.push($scope.votes)

      $scope.storeProductsInCookie = function() {
        $cookies.put("invoices", JSON.stringify($scope.newVotes))
      };
      $scope.getProductsInCookie = function() {
        console.log($cookies.get("invoices"));
      }

      $scope.storeProductsInCookie();
      $scope.getProductsInCookie();

    });
  </script>
</body>


</html>