How To Use LocalStorage In AngularJS

Posted by Weston Ganger on March 07, 2016

With all apps these days local storage can be used for anything. Its awesome.

My favourite is to use the ngStorage module for both local storage and session storage.

The simple alternative is to create a small angular service to interact which works perfect too.

yourApp.factory('$localstorage', ['$window', function($window) {
  return {
    set: function(key, value) {
      $window.localStorage[key] = value;
    get: function(key, defaultValue) {
      return $window.localStorage[key] || defaultValue || false;
    setObject: function(key, value) {
      $window.localStorage[key] = JSON.stringify(value);
    getObject: function(key, defaultValue) {
      if($window.localStorage[key] != undefined){
          return JSON.parse($window.localStorage[key]);
        return defaultValue || false;
    remove: function(key){
    clear: function(){

Related External Links:

Posted in Angular and Tagged with local-storage angularjs 

Want me to help develop your next project or application?

Contact Us

Specializing in Ruby-on-Rails, Javascript, Multi-Platform Electron Desktop Apps and Hybrid Cordova Mobile Apps.

Recommended Posts