Haradzieniec Haradzieniec - 1 year ago 163
AngularJS Question

Read from sessionStorage by ngStorage AngularJS that was saved by another library

In the browser session storage we have

`mykey` `myvalue`

where mykey is in the Key column of the Session Storage section
where myvalue is in the Value column of the Session Storage section
Everything's clear.

ngStorage (an AngularJS module) implemented in the project and works fine - writes and reads values that was written by ngStorage. However it stores value not as

mykey myvalue

but as

ngStorage-mykey "myvalue"

(two different things he does - adds prefix
and wraps myvalue with "").

The question is: how to read session value by ngStorage that was saved by another (not an AngularJS module)?

As result,
won't get
at all that was saved not by ngStorage.

It is possible without hacks to change the prefix ngStorage- with anything else, but another problem is "" wrappers.

So, any ideas how to read the values by ngStorage that were set NOT by ngStorage (that means without any prefixes prefix and not wrapped by "" )?

Again, by default, if you'd like to save
Hello World
text by $sessionStorage.message = "Hello World"; then ngStorage will add prefix by default (may be changed) and will add " " to the value like this:

"Hello World"

But I need to read
Hello World

Any ideas how to do that?

Answer Source

By default the serializer / deSerializer used by ngStorage are angular.toJson / angular.fromJson. This is configurable through the provider.

angular.toJson would wrap a string with ".

You can do this in your module config after injecting $sessionStorageProvider,



function mySerializer(obj) {
    if(angular.isObject(obj)) {
        return angular.toJson(obj);

    return obj;

function myDeserializer(str) {
    if(str && str.charAt(0) === '"') {
        return angular.fromJson(str);

    return str;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download