Javascript AngularJS:将对象存储在 cookie 中,给出 [Object Object] 的结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30662405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS: Storing an object in a cookie giving result of [Object Object]
提问by user3407039
I am trying to store a group of user credentials in a cookie when sending in this user object to my service -
将此用户对象发送到我的服务时,我试图将一组用户凭据存储在 cookie 中 -
this.SetCookie = function (user) {
$cookies.user = user;
}
However what I get when I try to retrieve this cookie I do not get an object but just a string that says "[Object Object]"
然而,当我尝试检索这个 cookie 时,我得到的不是一个对象,而是一个字符串,上面写着“[Object Object]”
I can store all the user credentials individually in their own cookies, as I know I can make that work, but it seems pretty inefficient? Is there an easy fix for this? The top result I found for this problem was related to JQuery and did not work for me.
我可以将所有用户凭据单独存储在他们自己的 cookie 中,因为我知道我可以做到这一点,但似乎效率很低?有没有简单的解决方法?我为这个问题找到的最高结果与 JQuery 相关,对我不起作用。
采纳答案by Pankaj Parkar
You are printing object directly that will always show [Object Object]because it contains an object of JSON, while printing it alert method it uses .toString()method to show it in console.
您正在直接打印对象,该对象将始终显示,[Object Object]因为它包含一个对象JSON,而打印它时,它使用 alert 方法.toString()在控制台中显示它。
You need to make that JSON.stringify(user)that will convert all the JSONobject to string. While using that object you need to use JSON.parse(user)that will convert object to JSON
您需要JSON.stringify(user)将所有JSON对象转换为字符串。在使用该对象时,您需要使用JSON.parse(user)它将对象转换为JSON
回答by sigmapi13
In Angular 1.4 I found that storing a JSON object by creating the cookie like this:
在 Angular 1.4 中,我发现通过像这样创建 cookie 来存储 JSON 对象:
var obj = {
currentUser: {
username: "testUN",
authdata: authdata
}
};
$cookies.putObject('cookieName', obj);
Allows you to get the cookie back like this:
允许您像这样取回 cookie:
var cookieWObject = $cookies.getObject('cookieName');
Then get to the values like this:
然后得到这样的值:
var username = cookieWObject.currentUser.username;
var authdata = cookieWObject.currentUser.authdata;
回答by Ashish Kadam
For using cookies in angular must be inject dependency 'ngCookies' at your controller or declare in angular.module('app',[ngCookies]), do not used $cookieStore is deprecated.
要在 angular 中使用 cookie,必须在您的控制器中注入依赖项 'ngCookies' 或在 angular.module('app',[ngCookies]) 中声明,不推荐使用 $cookieStore。
var app = angular.module('myApp',['ngCookies']);
app.controller('cookiesController',['$scope','$cookies',function($scope,$cookies){
//using put method you can add value using key value
$cookies.put('Kye','Admin');
//access cookies value
console.log($cookies.get('Key'));
//You can add also object in Cookies
var data = {
'name':'admin',
'pass':'admin'
}
$cookies.putObject('data',data);
//access object value
console.log($cookies.getObject('data'));
}]);
回答by puneet
JSON.stringify(user); //for storing in cookies
JSON.parse($cookies.user); //for converting into an object

