javascript 未捕获的错误:[$injector:unpr] Unknown provider Ionic Framework/AngularJS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25060602/
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
Uncaught Error: [$injector:unpr] Unknown provider Ionic Framework/AngularJS
提问by Ian
Following the local storage tutorial on the Ionic blog, I'm trying to set/get a localStorage value when my Ionic app runs but I get the error message:
按照Ionic 博客上的本地存储教程,我尝试在 Ionic 应用程序运行时设置/获取 localStorage 值,但收到错误消息:
Uncaught Error: [$injector:unpr] Unknown provider: $localstorageProvider <- $localstorage
My app.js code:
我的 app.js 代码:
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])
.run(function($ionicPlatform, $localstorage) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
$localstorage.set('name', 'Ian');
console.log($localstorage.get('name'));
});
})
And services.js:
和 services.js:
angular.module('starter.services', [])
.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] || '{}');
}
}
}]);
Not sure what I'm missing here.
不确定我在这里缺少什么。
EDIT: If I change my app.js code to the following, it works as expected:
编辑:如果我将 app.js 代码更改为以下内容,它会按预期工作:
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])
.run(function($ionicPlatform, $localstorage) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
window.localStorage.setItem('name', 'Ian');
console.log(window.localStorage.getItem('name'));
});
})
采纳答案by Ian
Circling back to this, I was able to fix it by injecting $window:
回过头来,我能够通过注入 $window 来修复它:
.run(function($ionicPlatform, $window ) {
Then I was able to get/set by using:
然后我可以使用以下方法获取/设置:
$window.localStorage.setItem( 'name', 'Ian' );
$window.localStorage.getItem( 'name' ); // Ian
回答by gabberr
Had the same problem with the Ionic tutorial implementation: http://learn.ionicframework.com/formulas/localstorage/
与 Ionic 教程实现有同样的问题:http: //learn.ionicframework.com/formulas/localstorage/
It worked when I removed the $ sign in front of localstorage.
当我删除 localstorage 前面的 $ 符号时,它起作用了。
.run(function($ionicPlatform, localstorage)