marcj / angular2-localstorage

Angular 2+ decorator to save and restore variables/class properties to HTML5 LocalStorage automatically.
303 stars 107 forks source link

Timing of key creation causing invalid properties #49

Open digital216 opened 8 years ago

digital216 commented 8 years ago

OK, that title was probably confusing as hell. Here's what I'm seeing.

The properties are working, for the most part. But the properties are not created in the storage containers (either local or session) when the site is first loaded. Instead, something has to happen to trigger the key creation.

For example. I have a checkbox, attached to a LocalStorage property. If I clear out my local storage keys for my site, then reload the site, there are no keys in the store. Then, when I check the box, the keys are created, however they are created with the default values instead of current values (I'm defaulting the value to false, but with the checkbox ticked, it should be true). Then, when I uncheck the box, the key stays false. Finally, when I check it again, it switches to true.

Steps to recreate: 1) Create a LocalStorage property of a boolean type, set it's default value to false. 2) Bind that value to a checkbox input through [(ngModel)]. 3) Load your site in a browser. 4) Open dev tools and look for the Resources (Chrome) or Storage (Firefox) tabs. 5) Delete the key if it already exists 6) Reload the site, you will notice the key doesn't exist 7) Check the box bound to the key, you'll see the key appear, but with the wrong (default) value

I can grab some screens or anything you need to see to help out.