javascript $window.sessionStorage 与 $cookieStore
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24069990/
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
$window.sessionStorage vs $cookieStore
提问by oberger
What is the difference between using $cookieStore and &window.sessionStorage? Are there times when one should be used over the other? Security Issues?
使用 $cookieStore 和 &window.sessionStorage 有什么区别?是否有时应该使用一个而不是另一个?安全问题?
Here is what I know so far:
以下是我目前所知道的:
The AngularJS docs state that the $cookieStore service is backed by "session cookies" (https://docs.angularjs.org/api/ngCookies/service/$cookieStore). So, it appears that information stored with $cookieStore is tied to the window/tab where it is used. This is affirmed by the use of the mysterious $browser service in the code for $cookieStore: https://github.com/angular/angular.js/blob/master/src/ngCookies/cookies.js#L125.
AngularJS 文档声明 $cookieStore 服务由“会话 cookie”(https://docs.angularjs.org/api/ngCookies/service/$cookieStore)支持。因此,使用 $cookieStore 存储的信息似乎与使用它的窗口/选项卡相关联。这通过在 $cookieStore 的代码中使用神秘的 $browser 服务得到证实:https: //github.com/angular/angular.js/blob/master/src/ngCookies/cookies.js#L125。
However, since $browser is an internal service and subject to change, I can't see how it is storing the data, to see if it is similar to sessionStorage.
然而,由于 $browser 是一个内部服务并且可能会发生变化,我看不到它是如何存储数据的,看看它是否类似于 sessionStorage。
The same browser/tab/window scope seems to apply to $window.sessionStorage (Scope of sessionStorage and localStorage).
相同的浏览器/选项卡/窗口范围似乎适用于 $window.sessionStorage (sessionStorage 和 localStorage 的范围)。
回答by David Boike
$cookieStore using session cookies means that data is persisted as cookies that are scoped to the session, i.e. not persistent. A cookie is scoped to the particular domain it is registered on, but may be shared between subdomains. The big deal about the cookie store is that those cookie values will be sent to the server for any requests to that domain. It would be shared between windows and tabs in the same session on the same domain.
使用会话 cookie 的 $cookieStore 意味着数据作为会话范围内的 cookie 持久化,即非持久化。cookie 的范围仅限于它注册的特定域,但可以在子域之间共享。关于 cookie 存储的大问题是那些 cookie 值将被发送到服务器以用于对该域的任何请求。它将在同一域上的同一会话中的窗口和选项卡之间共享。
$window.sessionStorage just accesses the window.sessionStorage, which really has nothing to do with Angular. Accessing it through $window just gives you the ability to test more easily using a mocked version of $window. Session storage is scoped to the current window, so unlike cookies, if you open a new tab to the exact same URL it will be a new sessionStorage object. There is also more room for storage than cookies. A cookie is limited to 4K, sessionStorage can differ per browser but is usually around 5MB.
$window.sessionStorage 只是访问window.sessionStorage,与Angular 真的没有关系。通过 $window 访问它只是让您能够使用 $window 的模拟版本更轻松地进行测试。会话存储的范围仅限于当前窗口,因此与 cookie 不同,如果您打开一个指向完全相同 URL 的新选项卡,它将是一个新的 sessionStorage 对象。还有比饼干更多的存储空间。cookie 限制为 4K,sessionStorage 可能因浏览器而异,但通常约为 5MB。
There's also window.localStorage (or $window.localStorage) which is basically the same as sessionStorage, except it is scoped by domain (two tabs can share the same data - there's even a storage event so you can find out when another tab changes it) and persists when you close your browser.
还有 window.localStorage(或 $window.localStorage),它与 sessionStorage 基本相同,但它是按域限定范围的(两个选项卡可以共享相同的数据 - 甚至还有一个存储事件,因此您可以找出另一个选项卡何时更改它) 并且在您关闭浏览器时仍然存在。