admin管理员组文章数量:1415664
I'm working with angular.js and ngstore. I'm using a token based authentication with a node.js REST service, from the angular app I send my credentials to rest server and I receive the token, I then store this token in $localStorage to use throughout all the angular app($localStorage.token). But it turns that sometimes $localStorage.token is undefined, even when I assigned the token to it, so when I call another rest endpoint sending the token in the headers, I'm actually sending an undefined value. Also when I try to logout I do
delete $localStorage.token
but when I check if the user has been loggedout actually the token still there. What is strange is that if I set breakpoints right after deleting the token or assigning the token and wait for a while, everything works, that's making me think that those operations may be asynchronous?
Is this a mon problem? How could I fix this?
Any help would appreciated, Thanks.
EDIT: actually I found that the problem is when using window.location, if I use $location.path it's working, but for certain reasons I need to use window.location, and it should work as far as I know
I'm working with angular.js and ngstore. I'm using a token based authentication with a node.js REST service, from the angular app I send my credentials to rest server and I receive the token, I then store this token in $localStorage to use throughout all the angular app($localStorage.token). But it turns that sometimes $localStorage.token is undefined, even when I assigned the token to it, so when I call another rest endpoint sending the token in the headers, I'm actually sending an undefined value. Also when I try to logout I do
delete $localStorage.token
but when I check if the user has been loggedout actually the token still there. What is strange is that if I set breakpoints right after deleting the token or assigning the token and wait for a while, everything works, that's making me think that those operations may be asynchronous?
Is this a mon problem? How could I fix this?
Any help would appreciated, Thanks.
EDIT: actually I found that the problem is when using window.location, if I use $location.path it's working, but for certain reasons I need to use window.location, and it should work as far as I know
Share Improve this question edited Feb 22, 2015 at 18:59 Jorge asked Feb 22, 2015 at 17:31 JorgeJorge 331 silver badge4 bronze badges2 Answers
Reset to default 5I had the same problem today, and using the following mit worked for me.
https://github./raynode/ngStorage
There is a helpful discussion about this problem here:
https://github./gsklee/ngStorage/issues/39
In this version of ngStorage, the author has thoughtfully provided a way to "save" before proceeding and performing a $window.location.reload();
During login:
$localStorage.value = 100;
$localStorage.$save();
(and then) $window.location.reload();
During logout:
delete $localStorage.value;
$localStorage.$save();
$window.location.reload();
This worked for me to ensure that the $localStorage variables were deleted before page reload.
No, local storage is not asynchronous.
JavaScript is a single thread environment and read/write operations to local storage occurs immediately.
Both session and local storage containers are the same. Except that session storage is a key/value pair with an expire timestamp specified.
Storage in HTML5 is not a reliable resource. As there are a number of browser states that will restrict or remove storage.
You are referring to ngStore
which is a module I've never heard of. There is no local storage module that is included with AngularJS by default, and there are multiple open source modules that handle this.
What is most likely happening is that you are handling a session token as a state variable instead of a state promise. When you use a variable to hold the state of a resource on the server, then that state has 3 possible values. 1 not assigned, 2 pending assignment and 3 value assigned.
Instead of reading the token from storage directly. You should have a service that returns a promise that will resolve to provide that token. This promise will only resolve after the REST operation has pleted.
本文标签: javascriptDoes ngStorage perform operations asynchronouslyStack Overflow
版权声明:本文标题:javascript - Does ngStorage perform operations asynchronously? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745199387a2647297.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论