javascript 不能使用 angular-local-storage
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25488033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't use angular-local-storage
提问by john Smith
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
我收到异常 - FF 浏览器中的未知提供程序 localstorageservice
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
回答by Radim K?hler
We should ask inejctor for 'localStorageService'
instead of your 'LocalStorageModule'
我们应该问 inejctor 'localStorageService'
而不是你的'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()
我们应该使用 .set()
// To add to local storage
localStorageService.set('localStorageKey','Add this!');