deviceReady how to


#1

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()">

ngCordova not working properly
#2

Ionic has a wrapper that does that for you:

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

#4

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>

#5

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.


#7

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…


#8

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.


#10

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 =)


#11

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);
});

#12

How to initialize sqlite plugin here


#13

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


#14

Use as it —

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