调用 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>rnrn<p>description ......... </p>rn","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":{}}
然后我使用以下角度功能将这些数据保存在 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']));
我尝试了anglar.toJson和json.parse函数。没有什么可以给我json格式的数据。此外,我尝试使用cookie存储来保存角度数据。
$cookieStore.put('recentx',$scope.twotap_builtin_cart);
然后我尝试从饼干商店打印。
console.log($cookieStore.get('recentx'));
它给了我输出[对象对象]。我不能用这个。我需要 json 数据用于 ng 重复目的我不能把对象(键)或getObject(键)函数,因为,我使用的角度版本大于1.4
字符串,然后使用 JSON.parse(t) 将其转换为 JSON。
或者尝试angular.toJson(t)
第一个保存在 cookie 中的人
var x= angular.toJson($scope.twotap_builtin_cart);
$cookies['xxx']= x;
从饼干中检索
var t = $cookies['xxx'];
var pota = angular.fromJson(t);
console.log(pota);
它应该适用于所有类似格式的 json 数据。但就我而言,数据太大了。所以我无法保存在饼干里面。现在我正在使用本地存储。首先使用$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);
}