-1

调用 api 后,我得到了如下的 json 数据,我使用变量存储了这些数据。

$scope.twotap_builtin_cart = {"sites":{"571fb46730bb1f373d00bdb4":{"info":{"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>description ......... </p>\r\n","add_to_cart":{"a6eda98b2e60363a67efb985ef622cea":{"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"],"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":{}}

然后我使用以下 angular 函数将这些数据保存在 cookie 中。

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

现在,当我尝试从 cookie 中检索这些数据时,它显示我是一个对象。我试图在控制台内打印它显示对象对象。但我需要 json 格式的数据。

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

我尝试了 angular.toJson 和 json.parse 函数。没有什么可以给我 json 格式的数据。此外,我尝试使用 cookie 存储来保存角度数据。

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

然后我尝试从 cookie 商店打印。

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

它给了我输出[object Object]。我不能用这个。我需要 json 数据用于 ng-repeat 目的我不能 putObject(key) 或 getObject(key) 函数,因为我使用的角度版本大于 1.4

4

2 回答 2

0

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

或尝试angular.toJson(t)

于 2016-07-13T06:45:05.457 回答
0

首先保存里面的cookies

 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 数据。但就我而言,数据太大了。所以我不能保存里面的cookies。现在我正在使用本地存储。首先使用 $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);
        }       
于 2016-07-18T04:25:13.877 回答