I'm using ionic and angular for building a mobile app. But I'm getting the following errors every time I integrate angular-cache in the app:
Here's what my index.html
file looks like:
<!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>
Then on my app.js
file I set angular data as one my dependencies:
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');
});
I've put the logic for logging in in my userService.js
file:
(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
});
};
})();
And if it helps, here's my config.xml
file:
<?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>
It seems to work fine when I just run it from the browser. But as soon as I upload to phonegap build and install the app on Genymotion, I get those errors. If I don't use angular cache on code, it also works fine.
Any ideas what could I be doing wrong? Do I need to install any plugins or edit the config.xml
file to make this work? Thanks in advance
Please remove your service declaration out of the (function(){ declaration :
angular.module('starter.services')
.service('UserService', ['$http', '$q', '$ionicLoading', 'DSCacheFactory', UserService]);
function UserService($http, $q, $ionicLoading, DSCacheFactory){ .................
Hope this helps !!.