ngStorage icon indicating copy to clipboard operation
ngStorage copied to clipboard

localStorage will not updated (after reload the current value is not there)

Open Lioxen opened this issue 11 years ago • 16 comments

I have an application where I use ngStorage to save some time values which will decreased every second, but with ngStorage the current time value will not shown after reload (its only decreased by one). To demonstrate this I have created an plunker: http://plnkr.co/edit/d48NAdAoS03qzAi3N9ND?p=preview Just input a integer and submit. After a while reload the page. The ngStorage value is than just decreased by one, but the localStorage value has the correct time. Why is that?

Now some explanation why my code is a little more complex to demonstrate this behaviour: In my real app the timevalue is a json object with a few timevalues in it and in the timewaitingService the setWait function calcs the lowest one to show this to the audience. Also in the tick function every timevalue will be decreased by one every second. So I like to show this demonstration code as near as possible to my code in the real app. May its my fault, because it is my first app using angular js. May somebody can explain why the timevalue will not be updated every second, but if I use the plain localstorage function it will.

Lioxen avatar Feb 27 '14 09:02 Lioxen

Same here

akizor avatar Nov 05 '14 13:11 akizor

I have similar issue: $localStorage contain my value, but localStorage does not

milushov avatar Dec 04 '14 22:12 milushov

Same: localStorage saves value a, at some point I update it to value b (this works), then when I refresh it goes back to value a

emiidee avatar Apr 16 '15 00:04 emiidee

I'm having the same problem as emiidee, it does update to new value but it keeps giving the old value when I refresh, why is that?

SlashingEdge avatar Jun 06 '15 06:06 SlashingEdge

@SlashingEdge could you try using master ngStorage.js and provide a snippets where this happen for me to test? :)

egilkh avatar Jul 28 '15 20:07 egilkh

As the plnkr doesn't seem to work, anyone that can confirm this still is a issue?

egilkh avatar Aug 19 '15 18:08 egilkh

Same here, updating $localStorage.token every 30 mins. The value remains the same.

ghost avatar Aug 23 '15 10:08 ghost

I noticed this and will be looking into it. I didn't seem to have this problem when I used an object (which is how I usually use $localStorage). Ex:

$localStorage.awesome = { key: 'value' };

In which it seemed more consistent. Will look into it.

egilkh avatar Aug 23 '15 20:08 egilkh

I notice the behavior described above where updates stopped working if I have multiple $localStorage.$default({...}); calls with different defaults in different angular components.

For example, if ServiceA has var $storageA = $localStorage.$default({ a: "foor"}); and ServiceB has var $storageB = $localStorage.$default({ b: "bar"});.

I got around this by making my own service wrapper with a var $storage = $localStorage.$default({ a: "foo", b: "bar"});. Then the wrapper service had a method to getA() and getB().

Note sure if this is a correct usage, but it got around the issue for me.

bdkent avatar Sep 03 '15 16:09 bdkent

Thanks bdkent for the reports.

There should be no need to make extra services. And I'll continue working on it. But sadly my time has been limited these last weeks. But I'm still here and will have more time soon :)

egilkh avatar Sep 04 '15 10:09 egilkh

I found that for me this was an issue with $localstorage.$default.

My workaround was to replace x = $localstorage.$default({a: "foo"}) with x.a = $localstorage.a || "foo";

This seems to restore the expected behaviour, having replaced $default everywhere in my code.

RolandHeath avatar Sep 20 '15 23:09 RolandHeath

Thanks. Seems the whole of $default is a little screwed up.

egilkh avatar Sep 21 '15 08:09 egilkh

+1

Current workaround: $localStorage.a = $localStorage.a || 'foo';

omarmeky avatar Jul 04 '16 16:07 omarmeky

I've faced a similar issue and it happened because I was setting item using localStorage.setItem(key value) and tried to read data using $localStorage.key which, of course is not ok. I used localStorage.setItem because of the need to immediately catch QuotaExceededError. The fix was to read data using localStorage.getItem or call $localStorage.$sync() to sync the ngStorage with native localStorage .

emanuel-virca avatar Nov 16 '17 17:11 emanuel-virca

i have same issue regarding this local storage first i have x value when i go to previous page and get y value i am getting y value.but, i need to be displayed with x and y value

pruthvim96 avatar Jan 31 '18 10:01 pruthvim96

Same: localStorage saves value a, at some point I update it to value b (this works), then when I refresh it goes back to value a

you can call a method in respective service regarding to localStorage like getRole(),getToken()..it return localStorage.getItem('key') so in your template(.html) ,you can call directly like authService.getToken() it returns updated value only. Before that you can provide dependency injection in your component. It works...

avimatrix avatar Feb 08 '19 07:02 avimatrix