如何为angularJS $cookies设置域

时间:2022-08-23 10:11:09

It seems $cookies only allows to set the Key/Value pair.

$cookies似乎只允许设置密钥/值对。

But i need to set the domain parameter as well. I need to set the cookie from one subdomain of website and use it from all subdomains of website.

但我也需要设置定义域参数。我需要从网站的一个子域名中设置cookie,并从网站的所有子域名中使用它。

I am using the angularJS and Twitter Bootstrap.

我正在使用angularJS和Twitter Bootstrap。

2 个解决方案

#1


8  

Currently this is not implemented in angular (1.2.7). You may have a look at angular.js line 4348 - the relevant code a little bit reduced:

目前这不是用角(1.2.7)来实现的。你可以看看角度。js行4348 -相关代码稍作删减:

if (value === undefined) {
   // Delete the cookie
   rawDocument.cookie = escape(name) + 
          "=;path=" + cookiePath + ";expires=Thu, 01 Jan 1970 00:00:00 GMT";
} else {
   // set or update the cookie value
   rawDocument.cookie = escape(name) + '=' + escape(value) + ';path=' + cookiePath);
}

as you can see the ;domain=XYZ is not included. You have to write your own cookiestore that implements this feature - but this should be very easy.

如您所见,不包含;domain=XYZ。您必须编写自己的cookiestore来实现这个特性——但是这应该非常简单。

#2


4  

I've created module for AngularJS to solve this issue:

我已经为AngularJS创建了一个模块来解决这个问题:

https://github.com/stevermeister/ng-biscuit

https://github.com/stevermeister/ng-biscuit

now you can simply set options:

现在你可以简单地设置选项:

cookieStore.put('x', 5, {domain: 'youdomain.com'})

#1


8  

Currently this is not implemented in angular (1.2.7). You may have a look at angular.js line 4348 - the relevant code a little bit reduced:

目前这不是用角(1.2.7)来实现的。你可以看看角度。js行4348 -相关代码稍作删减:

if (value === undefined) {
   // Delete the cookie
   rawDocument.cookie = escape(name) + 
          "=;path=" + cookiePath + ";expires=Thu, 01 Jan 1970 00:00:00 GMT";
} else {
   // set or update the cookie value
   rawDocument.cookie = escape(name) + '=' + escape(value) + ';path=' + cookiePath);
}

as you can see the ;domain=XYZ is not included. You have to write your own cookiestore that implements this feature - but this should be very easy.

如您所见,不包含;domain=XYZ。您必须编写自己的cookiestore来实现这个特性——但是这应该非常简单。

#2


4  

I've created module for AngularJS to solve this issue:

我已经为AngularJS创建了一个模块来解决这个问题:

https://github.com/stevermeister/ng-biscuit

https://github.com/stevermeister/ng-biscuit

now you can simply set options:

现在你可以简单地设置选项:

cookieStore.put('x', 5, {domain: 'youdomain.com'})