javascript – ngStorage和$window.localStorage之间的区别

前端之家收集整理的这篇文章主要介绍了javascript – ngStorage和$window.localStorage之间的区别前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
ngStorage和$window.localStorage有什么区别?什么时候更好地使用一个而不是另一个?
我必须选择其中一个用于Web应用程序.我必须保存配置文件用户和令牌的数据

解决方法

这是正常的 html5本地存储:

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

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

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

要检索数据,您可以执行类似的操作

localStorage.getItem("lastname");

因此,如果你想在角度中这样做,你会使用$window服务,但它的行为与上面的例子相同.

Source

解决ngStorage:

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:

$scope.$storage = $localStorage;

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

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

Source

如果您将在webapp中使用angularjs,我会使用ngStorage,因为您会更熟悉并熟悉语法.这只是我的观点.

原文链接:https://www.f2er.com/js/159594.html

猜你在找的JavaScript相关文章