Difference between ngStorage and $window.localStor

2019-03-27 16:35发布

问题:

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

回答1:

This is normal html5 local storage:

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.

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

window.localStorage - stores data with no expiration date

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"); 

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.

Source


To address 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

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

$scope.$storage = $localStorage;

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>

Source


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.



回答2:

The variable $window usually is the global window variable. The reason that Angular recommends using $window is because sometimes you would like to mock or replace the "real" window object (for testing purposes, for example).

Said this, using $window.localStorage means that you are using the Local Storage's vanilla API, while ngStorage is

An AngularJS module that makes Web Storage working in the Angular Way. Contains two services: $localStorage and $sessionStorage

Source here



回答3:

Just be mindful ngStorage internally uses an Angular watch to monitor changes to the $storage/$localStorage objects i.e a digest cycle is required to persist your new values reliably into the browser's local storage. Normally not a problem but if you store a value in $localStorage and open a new tab without a digest cycle occurring, you might not be able to see the stored values in your newly opened tab/window.

Ran into this problem on IE and had to use window.localStorage to get around it.



回答4:

var setLocalStorage = function (token, uname)
{
$localStorage.token = token;
$localStorage.name = uname;

}
$timeout(setLocalStorage(token, userForm.uname), 500);

Module Used : ngStorage

It works!