【问题标题】:Angular $cookies saved as Object. Can not fetch the object as JSONAngular $cookies 保存为对象。无法将对象作为 JSON 获取
【发布时间】:2016-11-15 14:16:24
【问题描述】:

调用 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

【问题讨论】:

  • 请分享一个小提琴,以便我们重现它并帮助您

标签: node.js json angularjs cookies cookiestore


【解决方案1】:

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

或者试试angular.toJson(t)

【讨论】:

  • var j= JSON.stringify(t);控制台.log(JSON.parse(j));打印的 [object Object]
【解决方案2】:

首先保存在里面的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);
        }       

【讨论】:

    猜你喜欢
    • 2015-11-11
    • 2023-04-10
    • 2010-12-02
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 2017-06-26
    相关资源
    最近更新 更多