Can't use angular-local-storage

2019-04-26 18:43发布

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

1条回答
冷血范
2楼-- · 2019-04-26 19:26

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:

we should use .set()

// To add to local storage
localStorageService.set('localStorageKey','Add this!');
查看更多
登录 后发表回答