Skip to content Skip to sidebar Skip to footer

Dynamic Data Needing To Store As Json Stringify With Localstorage In Angularjs Project

I have app in angularjs 1.5.x that i save dynamically db driven question answers to localstorage . Currently i'm doing it without a JSON format, which i don't like .factory('s

Solution 1:

Why not just keep a copy of the json in ram ?

.factory('sessionService', ['$http', function ($http) {

var temp = {};

    return {
        set: function (value) {
            temp = value;
            returnlocalStorage.setItem("json_key", JSON.stringify(temp));
        },
        get: function () {
            temp = JSON.parse(localStorage.getItem("json_key"));
            return temp;
        },
        destroy: function (key) {
            returnlocalStorage.removeItem("json_key");
        },
        update: function(key, value) {
            temp.key = value;
            set(temp);
            return temp;
        }
    };
}]);

Here "json_key" is the string key for getting the value in the localStorage.

temp is the ram copy at any moment of the local storage.

You can init temp with get.

update return the new value.

value should be the new json (I mean the whole json you want to store).

Post a Comment for "Dynamic Data Needing To Store As Json Stringify With Localstorage In Angularjs Project"