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

查看:176
本文介绍了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"); 

因此,如果要按角度进行此操作,则可以使用$ 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:

一个使Web存储以Angular方式工作的AngularJS模块.包含两个服务:$ 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和其他角度变量一起使用

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天全站免登陆