I've been experimenting with global variables in my ionic/angular + phonegap app project by implementing a factory service.
However, even adding a simple factory service like the one below somehow disrupts the app and causes all screens to turn completely white.
.factory('serviceName', function() {
return {}
})
I have two JavaScript files named app.js
and controller.js
This is how app.js looks like, with more states:
// Ionic Starter App
// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers'])
//TRIED ADDING FACTORY HERE
.run(function($ionicPlatform) {
$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);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
});
})
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
url: '/app',
abstract: true,
templateUrl: 'templates/menu.html',
controller: 'AppCtrl'
});
$urlRouterProvider.otherwise('/app/playlists');
});
My controller.js
somewhat resembles this, with various variables and functions:
angular.module('starter.controllers', [])
//ALSO TRIED ADDING FACTORY HERE
.controller('AppCtrl', function($scope, $ionicModal, $timeout,$http) {
// With the new view caching in Ionic, Controllers are only called
// when they are recreated or on app start, instead of every page change.
// To listen for when this page is active (for example, to refresh data),
// listen for the $ionicView.enter event:
//$scope.$on('$ionicView.enter', function(e) {
//});
// Form data for the login modal
$scope.loginData = {};
})
.controller('PlaylistsCtrl', function($scope) {
$scope.playlists = [
{ title: 'Reggae', id: 1 },
{ title: 'Chill', id: 2 },
{ title: 'Dubstep', id: 3 },
{ title: 'Indie', id: 4 },
{ title: 'Rap', id: 5 },
{ title: 'Cowbell', id: 6 }
];
})
.controller('PlaylistCtrl', function($scope, $stateParams) {
})
I found guidance from this resource in starting to use factories in my project.
While I am aware that I can utilize $rootScope, I am hesitant due to the drawbacks associated with global variables. Therefore, I seek assistance in implementing factories. Additionally, any tips on debugging with phonegap would be greatly appreciated. I currently use the phonegap development app on android and monitor the console for errors while working on it.