AngularJS - 如何设置到期日在AngularJS饼干 [英] AngularJS - How to set expiration date for cookie in AngularJS

查看:219
本文介绍了AngularJS - 如何设置到期日在AngularJS饼干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我们希望保存浏览器的cookie中的用户的授权信息,这不应该刷新(F5)丢失。


  2. 我们要存储的永久cookie的授权信息的情况下,用户选择了在日志上的时间记住我复选框。



解决方案

有关1.4:
如这里的评论和其他人以下,为1.4指出,角的原生支持cookie现在提供了编辑Cookie的功能:


  

由于38fbe3ee,$饼干将不再公开属性
  再present当前的浏览器cookie的值。 $饼干不再投票
  该浏览器更改cookie和不再拷贝的cookie
  值到$饼干对象。


  
  

这是改变,因为投票是昂贵的,导致的问题
  与$饼干性能无法与正确同步
  实际浏览器cookie的值(原因轮询原是
  添加是允许不同的选项卡之间的通信,但也有
  更好的方法来做到这一点今天,例如localStorage的。)


  
  

在$饼干新的API如下:


  
  

让把getObject方法putObject GETALL删除您必须显式使用
  上述方法,以访问cookie数据。这也意味着你
  再也不能收看$饼干性能检测的更改
  发生在浏览器中的cookies。


  
  

一般只需要这个功能,如果一个第三方库是
  在运行时编程方式更改的cookie。如果你靠这个
  那么您必须编写code,可以向第三方作出反应
  库进行更改,以饼干或实现自己投票
  机制。


实际的实现是通过 $ cookiesProvider 对象来完成,这可以通过 把传递 调用。

有关1.3及以下:
对于那些你谁做你最好避免不必加载jQuery插件,这似乎是一个不错的替代品角 - 的 https://github.com/ivpusic/angular-cookie

  1. We want to store User's Authorization information in cookie which should not be lost upon refresh (F5) of browser.

  2. We want to store authorization info in "permanent-cookie" in case user has opted for "Remember Me" check box at the time of log-on.

解决方案

For 1.4: As noted in the comment here and others below, as of 1.4, Angular's native cookie support now provides the ability to edit cookies:

Due to 38fbe3ee, $cookies will no longer expose properties that represent the current browser cookie values. $cookies no longer polls the browser for changes to the cookies and no longer copies cookie values onto the $cookies object.

This was changed because the polling is expensive and caused issues with the $cookies properties not synchronizing correctly with the actual browser cookie values (The reason the polling was originally added was to allow communication between different tabs, but there are better ways to do this today, for example localStorage.)

The new API on $cookies is as follows:

get put getObject putObject getAll remove You must explictly use the methods above in order to access cookie data. This also means that you can no longer watch the properties on $cookies to detect changes that occur on the browsers cookies.

This feature is generally only needed if a 3rd party library was programmatically changing the cookies at runtime. If you rely on this then you must either write code that can react to the 3rd party library making the changes to cookies or implement your own polling mechanism.

The actual implementation is done via a $cookiesProvider object, which can be passed via the put call.

For 1.3 and below: For those of you who have done your best to avoid having to load jQuery plugins, this appears to be a nice replacement for angular - https://github.com/ivpusic/angular-cookie

这篇关于AngularJS - 如何设置到期日在AngularJS饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆