Skip to content Skip to sidebar Skip to footer

How To Load The .run Before The Controller

I'm doing an app in Cordova using a pre-populated db, I'm trying to put the result of a db search in a < select > but without using a ng-click in my html file (that is the on

Solution 1:

$ionicPlatform.ready is not the first thing to be executed in your ionic application. The $ionicPlatform.ready function is called to tell you that the cordova APIs are ready to be used (it is a wrapper for the cordova deviceReady event). This means that Angular will start bootstrapping your app as soon as the library is loaded without waiting for the $ionicPlatform.ready callback.

You can fix this by removing the ng-app="starter" tag from your html body declaration as this instructs angular to immediately bootstrap your application. And instead bootstrap the application manually after ionic.Platform.ready is fired and you have done all of your initialisation. Here a sample code

var ionicApp = angular.module('starter', ['ionic']);

ionicApp.controller('first-select', function($scope){
  $scope.select = function(){
    console.log("controller initialized");
    //  do your thing
  };
})

ionic.Platform.ready(function() {
  console.log("device ready!");

  // do your db init magic 

  angular.bootstrap(document.body, ['starter']);
});

This way, "device ready" log will always be written to the console before "controller initialised" log, and there will be no race conditions.

Note that you can't use the run block of the ionicApp module to bootstrap your application, because the run block is called after angular has bootstrapped the application. So if you have not bootstrapped it automatically it will never be called.

Post a Comment for "How To Load The .run Before The Controller"