ionicPlatform.ready and controllers

Hi,

I’m a bit confused about “when” things are executed in a ionic app.

I though app.run and ionicPlatform.ready would be the first things to run, but if I’m not wrong I think they’re not.

To test this, I have created a new app using ionic start, and added some console.log messages:

ionic start menuapp sidemenu
cd menuapp
ionic platform add android
# add console.log messages to app.js and controllers.js
cordova run android

And then I check the log:

adb logcat | grep CordovaLog
I/CordovaLog( 1339): Changing log level to DEBUG(3)
I/CordovaLog( 1339): Found start page location: index.html
I/CordovaLog( 1339): Changing log level to DEBUG(3)
I/CordovaLog( 1339): Found start page location: index.html
D/CordovaLog( 1339): file:///android_asset/www/js/app.js: Line 10 : app.run starting
D/CordovaLog( 1339): file:///android_asset/www/js/app.js: Line 25 : app.run done
D/CordovaLog( 1339): file:///android_asset/www/js/controllers.js: Line 4 : AppCtrl starting...
D/CordovaLog( 1339): file:///android_asset/www/js/controllers.js: Line 36 : AppCtrl done...
D/CordovaLog( 1339): file:///android_asset/www/js/controllers.js: Line 40 : PlaylistsCtrl starting...
D/CordovaLog( 1339): file:///android_asset/www/js/controllers.js: Line 49 : PlaylistsCtrl done...
D/CordovaLog( 1339): file:///android_asset/www/js/app.js: Line 12 : ionic.ready starting...
D/CordovaLog( 1339): file:///android_asset/www/js/app.js: Line 23 : ionic.ready finished

So, the controllers AppCtrl and PlaylistsCtrl are executed before ionic.ready has finished (or even started).

My question is… I need to initialize some services (ie, Analytics, ad serving…) before the controllers are executed. I thought $ionicPlatform.ready() was the place to do it, but am I wrong? Where should I do it?

Thanks for your help!
Iñaki

+1

Did you find some feedback about that?

I’m a bit confused. A service is a singleton class. Are you running code right after the class in initialized? If so … don’t :smile:

I would do this

  1. have each service listen to a event that will be broadcasted on the $root

    angular.module(‘myModule’)

    .service(‘MyService’, function($q,rootScope) {

     $rootScope.$on('event:ReadyToGo', function(e, rejection) {
         ReadyToGo();
     });     
     
     var ReadyToGo  = function() {
         console.log("yes ready to rock");
     }
    
    return {
         ReadyToGo : ReadyToGo
     } 
    

    })

  2. with the $ionicPlatform.ready() … broadcast the event

    $rootScope.$broadcast(‘event:ReadyToGo’);