我使用离子和角度来构建移动应用。但每次我在应用程序中集成角度缓存时,我都会收到以下错误:
这是我的index.html
文件的样子:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="lib/angular-cache/dist/angular-cache.min.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<script src="js/app.js"></script>
<script src="js/controllers/home-starter.js"></script>
<script src="js/controllers/home/loginController.js"></script>
<script src="js/controllers/home/signupController.js"></script>
<script src="js/services/services-starter.js"></script>
<script src="js/services/userService.js"></script>
</head>
<body ng-app="starter" animation="slide-left-right-ios7">
<ion-nav-view></ion-nav-view>
</body>
</html>
然后在我的app.js
文件中,我将角度数据设置为我的依赖项:
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'angular-data.DSCacheFactory'])
.run(function($ionicPlatform, DSCacheFactory) {
$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();
}
DSCacheFactory('UserCache', {
storageMode: 'localStorage',
deleteOnExpire: 'aggressive'
});
});
})
.config(function($stateProvider, $urlRouterProvider) {
// Ionic uses AngularUI Router which uses the concept of states
// Learn more here: https://github.com/angular-ui/ui-router
// Set up the various states which the app can be in.
// Each state's controller can be found in controllers.js
$stateProvider
.state('login', {
url: '/login',
templateUrl: 'templates/login.html',
controller: 'LoginController'
})
$urlRouterProvider.otherwise('/login');
});
我已将用于登录的逻辑放在我的userService.js
文件中:
(function(){
angular.module('starter.services')
.service('UserService', ['$http', '$q', '$ionicLoading', 'DSCacheFactory', UserService]);
function UserService($http, $q, $ionicLoading, DSCacheFactory){
var base_url = 'http://somewhere.com';
self.UserCache = DSCacheFactory.get('UserCache');
self.UserCache.setOptions({
onExpire: function(key, value){
login.then(function(){
}, function(){
self.UserCache.put(key, value);
});
}
});
function login(user){
var deferred = $q.defer();
var cache_key = 'user';
$ionicLoading.show();
$http.post(base_url + '/api/login', user)
.success(function(data){
$ionicLoading.hide();
self.UserCache.put(cache_key, data.user);
deferred.resolve(data);
})
.error(function(data){
deferred.reject();
});
return deferred.promise;
};
return({
login: login
});
};
})();
如果有帮助,请点击我的config.xml
文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.ionicframework.vestor507394" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>tester</name>
<description>
An Ionic Framework and Cordova project.
</description>
<author email="hi@ionicframework" href="http://ionicframework.com/">
Ionic Framework Team
</author>
<content src="index.html"/>
<access origin="*"/>
<preference name="webviewbounce" value="false"/>
<preference name="UIWebViewBounce" value="false"/>
<preference name="DisallowOverscroll" value="true"/>
<preference name="BackupWebStorage" value="none"/>
<feature name="StatusBar">
<param name="ios-package" value="CDVStatusBar" onload="true"/>
</feature>
</widget>
当我从浏览器运行它时似乎工作正常。但是一旦我上传到phonegap构建并在Genymotion上安装应用程序,我就会收到这些错误。如果我不在代码上使用角度缓存,它也可以正常工作。
任何想法我可能做错了什么?我是否需要安装任何插件或编辑config.xml
文件才能使其正常工作?提前致谢
答案 0 :(得分:0)
请从(function(){声明:
中删除您的服务声明angular.module('starter.services')
.service('UserService', ['$http', '$q', '$ionicLoading', 'DSCacheFactory', UserService]);
function UserService($http, $q, $ionicLoading, DSCacheFactory){ .................
希望这有助于!!。