• Home

Ionic framework and deviceready

As I was working on a mobile app, I came around on a - not so - strange problem, that may occurs very often when you are working with device plugins. As you may know the cordova/PhoneGap platform can use some devices features such as camera, microphone and so on... It can access some "software" features via the same plugin system. For example if you want to drop a line to the console or retrieve contacts from the contact list you should add a dedicated plugin (cordova|phonegap|ionic plugin add ....). Fine.

Ionic Framework provides us a nice $ionicPlatform.ready(function() {...}); in the default app.js. But if you want to use a plugin at the very begining of you app and as this function launches when the document.ready() event fires, your plugin will not be ready for use and will not be available for your functions in your view/controller.

You can find a good explanation about this problem on : http://java.dzone.com/articles/ionic-and-cordovas-deviceready

I was able to find a "workaround" this (can't call it a workaround since it is so simple, a "trick" is more appropriate) by adding a

$ionicPlatform.ready(function()  { /* add stuff here, even calls to a function defined in this block */ )};

Here is two controllers, the first one will need an action once the document.ready() event fires (such as a ng-click="findContact();" directive) the second one will be launched at the first loading :

The whole github repo for this trick is here : https://github.com/lennycartier/ionic-contactlist

Print Email