简体   繁体   中英

AngularJS and Cordova- Detect Device Ready

I've been working on an app using AngularJS. Now, I'm ready to take my app and bundle it up with Cordova. My issue is, I'm not sure how to handle the ondeviceready event. At this time, my app is setup like this:

index.html

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript" src="/app/app.js"></script>
</head>

<body>
  ...
  <script type="text/javascript">
    if (window.location.protocol === "file:") {
      document.addEventListener('deviceready', initializeApp(), false);
    } else {
      initializeApp();
    }
  </script>
</body>
</html>

app.js

function initializeApp() {
  var myApp = angular.module('myApp', ['ngRoute']);
  myApp.config(function() {
    ...
  });

  myApp.run(function() {
    ...
  });
}

I put the initialization in a function called initializeApp so I could reuse it. However, when I attempt to run this, I'm getting an error that says...

Uncaught ReferenceError: initializeApp is not defined 

What am I doing wrong?

I'm not sure what your exact issue is, but I know the following piece of code is incorrect

Change

document.addEventListener('deviceready', initializeApp(), false);

To

document.addEventListener('deviceready', initializeApp, false);

By doing it the first way, you're trying to execute the function instantly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM