How do I store data in local storage using Angularjs?

angularjs

Solution

this is a bit of my code that stores and retrieves to local storage. i use broadcast events to save and restore the values in the model.

app.factory('userService', ['$rootScope', function ($rootScope) {

    var service = {

        model: {
            name: '',
            email: ''
        },

        SaveState: function () {
            sessionStorage.userService = angular.toJson(service.model);
        },

        RestoreState: function () {
            service.model = angular.fromJson(sessionStorage.userService);
        }
    }

    $rootScope.$on("savestate", service.SaveState);
    $rootScope.$on("restorestate", service.RestoreState);

    return service;
}]);

Problem

Currently I am using a service to perform an action, namely retrieve data from the server and then store the data on the server itself. Instead of this, I want to put the data into local storage instead of storing it on the server. How do I do this?

Original source

Related problems