Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use angular-local-storage

This my code:

   angular.module('MyModule').controller('MyController', ['$scope', '$stateParams','$location', '$http','LocalStorageModule',
function($scope, $stateParams, $location, $http, localStorageService) {

        localStorageService.add('test', 'val');
]);

any Idea whats wrong?

I'm getting exception - unknown provider localstorageservice in the FF browser

Firebug Error

Error: [$injector:unpr] Unknown provider: LocalStorageModuleProvider <-   LocalStorageModule
http://errors.angularjs.org/1.2.20/$injector/unpr?p0=LocalStorageModuleProvider%20%3C-     %20LocalStorageModule
    at http://127.1.0.0/lib/angular/angular.js:78:12
like image 924
john Smith Avatar asked Aug 25 '14 14:08

john Smith


1 Answers

We should ask inejctor for 'localStorageService' instead of your 'LocalStorageModule'

So the code should look like this:

angular
    .module('MyModule')
    .controller('MyController', ['$scope', '$stateParams','$location'
                                , '$http'
                                // instead of this
                                // ,'LocalStorageModule',
                                // use this
                                ,'localStorageService',
               function($scope, $stateParams, $location, $http, localStorageService) 
    {
        //localStorageService.add('test', 'val');
        localStorageService.set('test', 'val');
    }]);

And when we do initialize the module, we have to include the local storage module

angular
    .module('MyModule', [
       'LocalStorageModule', 
        ...
    ])

And also as documented here:

  • Angular Local Storage

we should use .set()

// To add to local storage
localStorageService.set('localStorageKey','Add this!');
like image 192
Radim Köhler Avatar answered Sep 19 '22 13:09

Radim Köhler