I use the following solution, which allows AngularJS to load when working with Cordova, as well as when starting directly in the browser , where most of my development takes place. You need to remove the ng-app directive from the index.html main page since manual download is replaced.
UPDATE: Since then, I have moved on to the next method, which, in my opinion, is cleaner. It works with both ionic and vanilla cordovas / PhoneGap. This should be the last bit of JavaScript to run - perhaps inside the script tag before the / body tag.
angular.element(document).ready(function () { if (window.cordova) { console.log("Running in Cordova, will bootstrap AngularJS once 'deviceready' event fires."); document.addEventListener('deviceready', function () { console.log("Deviceready event has fired, bootstrapping AngularJS."); angular.bootstrap(document.body, ['app']); }, false); } else { console.log("Running in browser, bootstrapping AngularJS now."); angular.bootstrap(document.body, ['app']); } });
Here's the older solution I used:
// This is a function that bootstraps AngularJS, which is called from later code function bootstrapAngular() { console.log("Bootstrapping AngularJS"); // This assumes your app is named "app" and is on the body tag: <body ng-app="app"> // Change the selector from "body" to whatever you need var domElement = document.querySelector('body'); // Change the application name from "app" if needed angular.bootstrap(domElement, ['app']); } // This is my preferred Cordova detection method, as it doesn't require updating. if (document.URL.indexOf( 'http://' ) === -1 && document.URL.indexOf( 'https://' ) === -1) { console.log("URL: Running in Cordova/PhoneGap"); document.addEventListener("deviceready", bootstrapAngular, false); } else { console.log("URL: Running in browser"); bootstrapAngular(); }
If you are having problems with the http / https detection method, due to possibly downloading the Cordova application to your phone from the Internet, you can use the following method:
function bootstrapAngular() { console.log("Bootstrapping AngularJS"); // This assumes your app is named "app" and is on the body tag: <body ng-app="app"> // Change the selector from "body" to whatever you need var domElement = document.querySelector('body'); // Change the application name from "app" if needed angular.bootstrap(domElement, ['app']); } // This method of user agent detection also works, though it means you might have to maintain this UA list if (navigator.userAgent.match(/(iOS|iPhone|iPod|iPad|Android|BlackBerry)/)) { console.log("UA: Running in Cordova/PhoneGap"); document.addEventListener("deviceready", bootstrapAngular, false); } else { console.log("UA: Running in browser"); bootstrapAngular(); }
Note that you still need the same bootstrapAngular function from the first example.
Why manually bootstrap AngularJS with Cordova / PhoneGap / Ionic?
Some people who come here may not know why you would like to do this in the first place. The problem is that you may have AngularJS code that uses Cordova / PhoneGap / Ionic plugins and these plugins will not be ready until AngularJS starts, because Cordoba needs more time to get up and work on the device than the plain old Javascript code for AngularJS does.
So, in these cases, we need to wait until Cordova / PhoneGap / Ionic is ready before launching (self-tuning) AngularJS, so that Angular will have everything you need to run.
For example, let's say you use the NG-Persist Angular module, which uses local storage to save data in the browser, the iOS Keychain plugin when running on iOS, and the cordova-plugin-file when running on Android. If your Angular application tries to download / save something right off the bat, NG-Persist checking on window.device.platform (from the device plugin ) will fail, because the mobile code has not yet completed launch, and instead of your beautiful application, you will get white page only.