Angular $ cookies保存为Object。无法将对象作为JSON获取

时间:2016-07-13 06:27:58

标签: angularjs json cookies cookiestore

在致电api后我得到了以下数据

$scope.twotap_builtin_cart = {
       "sites":{
              "571fb46730bb1f373d00bdb4":{
                         "info":{"logo":"https://core.twotap.com/system/sites/logos/571f/b467/30bb/1f37/3d00/bdb4/small/571fb46730bb1f373d00bdb4.png?1468389436927",
                         "name":"Overstock",
                         "url":"overstock.com"
              },              
              "currency_format":"$AMOUNT",
              "coupon_support":false,
              "gift_card_support":false,
              "checkout_support":["noauthCheckout"],
              "shipping_countries_support":["United States of America"],
              "billing_countries_support":["United States of America"],
              "shipping_options":{"cheapest":"Default shipping option"},
              "returns":"<p><span style=\"font-size:16px\"><strong>Overstock Return Policy</strong></span></p>\r\n\r\n<p>You can initiate a return for most new and unopened items&nbsp;within 30 days of delivery for a full refund.* You must return items in new or unused condition with all original materials included with the shipment. We must receive your returned items at our processing facility within 45 days of delivery (see&nbsp;Product-Specific Return Policies&nbsp;for exceptions). We inspect all returned items and award a partial refund for opened or used products. If the return is a result of our error or defective product, we will refund the full cost of the merchandise and shipping charges.</p>\r\n\r\n<p>If you initiate a return after 30 days, we may issue a partial refund* depending on elapsed time and condition of merchandise.</p>\r\n\r\n<p><strong>Defective Product and Returns Due to Our Error</strong></p>\r\n\r\n<p>You may return defective items, items damaged at the time of receipt, and incorrect items received without penalty.** Your return shipping charges will be waived.</p>\r\n\r\n<p>When we receive your return, we inspect the item and provide a replacement or issue a refund. If the product is not defective, return shipping fees will apply and/or a partial refund may be issued.</p>\r\n\r\n<p><strong>Products Returned After 30 days</strong></p>\r\n\r\n<ul>\r\n\t<li>Because products devalue over time, we offer partial refunds for items returned over 30 days after delivery</li>\r\n\t<li>We may reduce the refund based on the condition of the merchandise</li>\r\n\t<li>We may also deduct the original and return shipping charges from your refund</li>\r\n\t<li>If we send you a replacement item, we must receive the original item at our warehouse within 30 days your return initiation. Otherwise, we will charge you full price for the replacement item</li>\r\n</ul>\r\n\r\n<p><em>For more about return policy rules and exceptions you can visit :&nbsp;https://help.overstock.com/app/answers/detail/a_id/1</em></p>\r\n",
              "add_to_cart":{"a6eda98b2e60363a67efb985ef622cea":{"original_url":"http://www.jdoqocy.com/click-3726866-10654383?url=http%3A%2F%2Fwww.overstock.com%2F7281112%2Fproduct.html%3FTRACK%3Daffcjfeed%26CID%3D207442%26fp%3DF","clean_url":"http://www.overstock.com/7281112/product.html?CID=207442",
              "weight":"1000",
              "status":"done",
              "required_fields":{
                    "quantity":{
                          "data":[{"input_type":"text","input_name":"INPUT"}]}},
                          "discounted_price":null,
                          "original_price":null,
                          "pickup_support":false,
                          "url":"http://www.overstock.com/7281112/product.html?TRACK=affcjfeed&CID=207442&fp=F","required_field_values":{},"required_field_names":["quantity"],"categories":["Sports & Toys","Toys & Hobbies","Games & Puzzles","Board Games"],"description":"<p>ITEM#: 14756475</p> From a different age when people had time to sit and play games together, the Britannia Games Compendium is packed full of fun. Based on a product from the 1940s the Britannia Games Compendium has something for all ages.\n<br><br>\n<ul>\n<li>Brand: Perisphere and Trylon</li>\n<li>The Britannia Compendium of Games</li>\n<li>Model: PTRG-1088</li>\n<li>Based on a product from the 1940s the Britannia Games Compendium has something for all ages</li> <li>Beautifully made and highly collectable</li>\n<li>Number of players: 1 or more</li>\n<li>Includes: Chess, Draughts, Dominoes, Pick-up Sticks, Snap, Tiddlywinks, Playing Cards, Muggins, Poker Dice, Horse Racing Game, one (1) multiple fold-out game board, and instructions</li>\n<li>Recommended for ages 5 years and older</li>\n</ul>","alt_images":["http://ak1.ostkcdn.com/images/products/7281112/Perisphere-and-Trylon-The-Britannia-Compendium-of-Games-f17b48f6-8c81-4a0e-af12-fa2e3d97ac25_320.jpg"],"image":"http://ak1.ostkcdn.com/images/products/7281112/Perisphere-and-Trylon-The-Britannia-Compendium-of-Games-f17b48f6-8c81-4a0e-af12-fa2e3d97ac25_600.jpg","price":"$37.49","title":"Perisphere and Trylon The Britannia Compendium of Games"}}}},"user_id":null,"unknown_urls":[],"cart_id":"5785d83c493bfb7f2a352cde","notes":null,"country":"us","stored_field_values":{},"message":"done","description":"AddToCart has been completed.","product_urls":[]}

然后我将这些数据保存在cookie中。

$cookies['xxx'] = $scope.twotap_builtin_cart; 

现在,当我尝试从cookie中检索此数据时,它将我显示为对象。我试图在控制台内打印它显示对象

var t = $cookies['xxx'];
console.log(t);
$scope.recentItemsx = angular.toJson(t);
console.log($scope.recentItemsx);
console.log(JSON.parse($cookies['xxx']));

我尝试过JSON.parse,anglar.toJson和jsonstringify。没有什么可以解决这个问题

我尝试过使用cookie商店

$cookieStore.put('recentx',$scope.twotap_builtin_cart);

我还在控制台中跟踪警告Cookie

&#39; recentx&#39;可能没有设置或溢出,因为它太大(6675> 4096字节)! 在我打印的另一个控制器中

console.log($cookieStore.get('recentx'));

它给我输出[object Object]。我无法解决这个问题。我需要json数据用于ng-repeat目的 我不能putObject或Get Object,因为它们的角度版本大于1.4

2 个答案:

答案 0 :(得分:0)

尝试使用JSON.stringify(t)将其转换为字符串,然后使用JSON.parse(t)将其转换为JSON。

或尝试angular.toJson(t)

答案 1 :(得分:0)

首先保存内部Cookie

 var x= angular.toJson($scope.twotap_builtin_cart);
 $cookies['xxx']= x;

从Cookie中检索

 var t = $cookies['xxx'];
 var pota = angular.fromJson(t);
 console.log(pota);

它适用于所有类似格式的json数据。但就我而言,数据太大了。所以我无法保存内部cookie。现在我正在使用localstorage。首先使用$ window服务并在控制器内注入$ window。 设置

  var ret= angular.toJson($scope.twotap_builtin_cart);
  $window.localStorage.setItem('recent',ret);

要检索

   if(angular.isDefined($window.localStorage.getItem('recent')));
        {
        var tem =$window.localStorage.getItem('recent');
        var pota = angular.fromJson(tem);
        console.log(pota);
        }