0

When I was working with angular cookies, I found out that only strings are accepted by $cookies and stored in browser's cookie when set. i.e

If I set the following cookies

$cookies.id = 12345;
$cookies.name = "Prasad";

Only $cookies.name is set in the browser. So I had to change it to

$cookies.id = "12345";

to make it work.

here is the fiddle link to show the demo.

I've tried to check the angular source code to see why and found that they are deliberately checking for the value to be isString.

Is there any specific reason why they are accepting only string values. Couldn't we just allow integers also and convert to string while adding to browser cookie.

Thanks!

4

1 回答 1

0

主要原因可能是 cookie 的值总是作为字符串存储在浏览器中。当然,Angular 可以允许您在使用服务时将其保存为 int,然后在创建 cookie 之前将其转换,问题是人们会假设当他们稍后读取该 cookie 时,值仍然是 int (毕竟他们就是这样保存的)。

它不会,它会是一个字符串。

Angular 似乎认为(我个人同意)最好在前面严格一些。强制输入与输出相同,这样人们在阅读 cookie 时就不会误解他们会得到什么。

于 2013-11-18T14:09:36.883 回答