How To Use LocalStorage In AngularJS

Posted by Weston Ganger

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:

Article Topic:Software Development - Angular

Date:March 07, 2016

Recommended Posts



Send Me A Message

I would love to get in touch and talk about your next project. Feel free to send a message and I will get back to you shortly.

Get Connected