Need help using localstorage please


I am following the example on this page

I put (copy/pasted) the following into the services.js file:

angular.module('ionic.utils', [])
  .factory('localstorage', ['$window', function($window) {
    return {
      set: function(key, value) {
        $window.localStorage[key] = value;
      get: function(key, defaultValue) {
        return $window.localStorage[key] || defaultValue;
      setObject: function(key, value) {
        $window.localStorage[key] = JSON.stringify(value);
      getObject: function(key) {
        return JSON.parse($window.localStorage[key] || '{}');

And (copy/pasted) the following into my controllers.js file:

var app = angular.module('myapp.controllers', ['ionic.utils'])$localstorage) {
    $localstorage.set('name', 'Max');
    $localstorage.setObject('post', {
      name: 'Thoughts',
      text: 'Today was a good day'

  var post = $localstorage.getObject('post');

But I get the following error:

Uncaught Error: [$injector:unpr] Unknown provider: $localstorageProvider <- $localstorage$injector/unpr?p0=%24localstorageProvider%20%3C-%20%24localstorage 

I probably don’t have things in the right place. What am I doing wrong?



Your factory name is ‘localstorage’ whereas the factory which you have injected is $localstorage hence the error. Just rename your factory to $localstorage and it should work


That was it. Thank you sir.


No worries :smiley: happy learning