deviceReady how to

Is there any specific way in angular to add deviceready event listener? I followed the phonegap docs and the snippet bellow is not working

<script>
 function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    function onDeviceReady() {
        alert("Device Ready")
    }

</script>

<body ng-app="app" animation="slide-left-right-ios7" onload="onLoad()">

Ionic has a wrapper that does that for you:

ionic.Platform.ready(function(){
   do something that requires the deviceready event ...
});
5 Likes

Thank you jough but I still cant get it work:(

For example I have a controller:

.controller('infoCtrl', function($scope) {
  ionic.Platform.ready(function() {
    var device = ionic.Platform.device();
    alert(device.platform);
  });
});

and it returns undefined when I run it on iPhone.

p.s. I have installed org.apache.cordova.device and my config.xml is

<feature name="Device">
    <param name="ios-package" value="CDVDevice" />
</feature>

Check out this post, which shows you how to set up cordova/phonegap features correctly

The suggested method is to define the ionic.Plateform.ready method as a module and working from there.

Do I actually have to run any commands after installing cordova’s plugins? I used “cordova prepare” after installing the device plugin and it actually made it work…

Running the cordova plugin install command is frequently all that is required. However, some plugins require settings to be added to the config.xml file. You need to review the Cordova/Phonegap docs for your specific plugin to see what is required depending on target device.

Hi! Try this:

.factory('cordova', function () {
  return {
	  test: function(){
		  document.addEventListener("deviceready", this.ready, false);
	  },
	  ready: function(){
		  //Save the world!
	  }

  }
})
.controller('Home', function($scope, cordova){
	cordova.test();
})

Works fine for me =)

Take a look my implementation

app.js

angular.run(function(.., CordovaService) {};

CordovaService.js

.service('CordovaService', function() {
  document.addEventListener("deviceready", function() {
    console.log('** cordova ready **');
  }, false);
});

How to initialize sqlite plugin here

I’ve tried this but CordovaService doesn’t runs by itself…

Use as it —

.controller(“myCtrl”, function($ionicPlatform){
$ionicPlatform.ready(function(){
// do something here
});
})