ngStorage 和 $window.localStorage 的区别 [英] Difference between ngStorage and $window.localStorage

查看:31
本文介绍了ngStorage 和 $window.localStorage 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ngStorage 和 $window.localStorage 有什么区别?什么时候用一个比另一个更好?我必须为 Web 应用程序选择其中之一.我必须保存个人资料用户和令牌的数据

What is the difference between ngStorage and $window.localStorage? When is it better to use one instead that the other? I have to choose one of them for a web app. I have to save data of profile user and the token

推荐答案

这是正常的html5本地存储:

借助本地存储,Web 应用程序可以在用户浏览器中本地存储数据.在 HTML5 之前,应用程序数据必须存储在 cookie 中,包含在每个服务器请求中.本地存储更安全,可以在本地存储大量数据,不影响网站性能.与 cookie 不同的是,存储限制要大得多(至少 5MB)并且信息永远不会传输到服务器.本地存储是每个源(每个域和协议).来自同一个源的所有页面都可以存储和访问相同的数据.

With local storage, web applications can store data locally within the user's browser. Before HTML5, application data had to be stored in cookies, included in every server request. Local storage is more secure, and large amounts of data can be stored locally, without affecting website performance. Unlike cookies, the storage limit is far larger (at least 5MB) and information is never transferred to the server. Local storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data.

它为您提供访问存储的对象 - window.localStorage 和 window.sessionStorage

It gives you to objects to access the storage - window.localStorage and window.sessionStorage

window.localStorage - 存储没有过期日期的数据

window.localStorage - stores data with no expiration date

window.sessionStorage - 存储一个会话的数据,因此当浏览器选项卡关闭时数据会丢失

window.sessionStorage - stores data for one session, so data is lost when the browser tab is closed

要检索数据,您可以执行以下操作

To retrieve data you would do something like this

localStorage.getItem("lastname"); 

因此,如果您想在 angular 中执行此操作,您可以使用 $window 服务,但它的行为与上面的示例相同.

So if you wanted to do this in angular you would use the $window service but it would behave the same as the examples above.

来源

要解决 ngStorage:

To address ngStorage:

一个 AngularJS 模块,它使 Web 存储以 Angular 方式工作.包含两个服务:$localStorage 和 $sessionStorage.无需像 $window 服务中那样处理 getter 和 setter

An AngularJS module that makes Web Storage working in the Angular Way. Contains two services: $localStorage and $sessionStorage. No dealing with getters and setters like you have to in $window service

您可以在 $scope 下通过引用传递 $localStorage 或 $sessionStorage :

You can pass $localStorage or $sessionStorage by reference under $scope:

$scope.$storage = $localStorage;

然后你可以使用 $storage as 和其他角度变量

Then you can use $storage as and other angular variable

<body ng-controller="Ctrl">
    <button ng-click="$storage.counter = $storage.counter + 1">{{$storage.counter}}</button>
</body>

来源

如果你将在你的 web 应用程序中使用 angularjs,我会使用 ngStorage,因为你会更舒服和熟悉语法.不过这只是我的意见.

If you will be working with angularjs in your webapp I would use ngStorage because you will be more comfortable and familiar with the syntax. That is just my opinion though.

这篇关于ngStorage 和 $window.localStorage 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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