Importance of .run(function($ionicPlatform)

Hi,

I am new to ionic and have successfully installed and tested my first tutorial app. I am facing the following problem while working around with this tutorial http://ionicframework.com/docs/guide/building.html

When I create a blank project ‘app.js’ is created with the following code in it:

 .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).
    // The reason we default this to hidden is that native apps don't usually show an accessory bar, at
    // least on iOS. It's a dead giveaway that an app is using a Web View. However, it's sometimes
    // useful especially with forms, though we would prefer giving the user a little more room
    // to interact with the app.
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      // Set the statusbar to use the default style, tweak this to
      // remove the status bar on iOS or change it to use white instead of dark colors.
      StatusBar.styleDefault();
    }
  });
});

However the following code won’t work unless I remove the above js code. So my question: Is it important to keep the above code? or can I do away with it? If it’s important how do I maintain both these codes.

.controller('TodoCtlr', function($scope){
  $scope.tasks = [
    {title: 'Collect Coins'},
    {title: 'Read books'},
    {title: 'Go home '},
    {title: 'have dinner'}
  ];
});

Thanks for your time.

Your code will work if you remove the ;from the last line of the run function code. This line is like the ending of your file, so if you have your controller after that it won’t be recognised. It should look like this:

// run function stuff...
})

.controller('TodoCtlr', function($scope){
// your controller
}); // Here should be the end now!
1 Like

Thanks that worked like a charm… :slight_smile: