在
local storage tutorial on the Ionic blog之后,我正在尝试在我的Ionic应用程序运行时设置/获取localStorage值,但是我收到错误消息:
Uncaught Error: [$injector:unpr] Unknown provider: $localstorageProvider <- $localstorage
我的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')); }); })
和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] || '{}'); } } }]);
不知道我在这里缺少什么.
编辑:如果我将我的app.js代码更改为以下代码,它按预期工作:
angular.module('starter',$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')); }); })
解决方法
与Ionic教程实现有同样的问题:
http://learn.ionicframework.com/formulas/localstorage/
http://learn.ionicframework.com/formulas/localstorage/
当我删除localstorage前面的$符号时,它工作正常.
.run(function($ionicPlatform,localstorage)