Initialize my angularJs App after Phonegap deviceready

后端 未结 5 1418
小蘑菇
小蘑菇 2020-12-08 11:45

I have a phonegap app using AngularJS.

On my app I am using the NetworkStatus plugin to confirm the type of connection the mobile phone is using.

On my root

相关标签:
5条回答
  • 2020-12-08 12:03

    Getting the injector module error from AngularJs usually means that you either mispelled the name of the module or angular simply did not find it.

    If the Angular app works properly on its own (e.g when not wrapped in phonegap), this means that this issue is in the order the things happens when your index.html is loaded.

    • Cordova/PhoneGap loads your index page
    • Its Webview parses it and loads its scripts tags
    • If some code is not wrapped in functions or objects, it's executed straight away
    • Phonegap sends the event deviceready to tell your app that its bridges with native code are ready

    The last 2 operations can occur in both orders but most often in the one I gave you. Thus, if you put your angular module name on the html or body tag through ng-app for instance, angular will try loading it when it finds it.

    So, for it to work, you need to :

    • Remove YourAppName from html/body tag
    • Create your angular module normally (its name must match in bootstrap and module calls)
    • Use the deviceready event as the trigger to boostrap your application

    For example (short example, nothing but css in head) :

    <body>
        <div class="app">
            <h1>PhoneGap</h1>
            <div id="deviceready" class="blink">
            {{2+2}}
            </div>
        </div>
        <script type="text/javascript" src="phonegap.js"></script>
        <script type="text/javascript" src="js/angular.min.js"></script>
        <script type="text/javascript">
            document.addEventListener('deviceready', function onDeviceReady() {
                angular.bootstrap(document, ['YourAppName']);
            }, false);
    
            var YourAppName = angular.module('YourAppName', []);
        </script>
    </body>
    

    If you want to understand this on your own, I recommend putting some console.log to get the order of things.
    You can aslo use Chrome DevTools remote debugging which works pretty well If you have Chrome 32+ on your pc and android 4.4 on phone, or only pc and you debug on emulator. Quite nice to see the errors and stuff. Debugging webviews is a bit strange at first but really useful to trace errors !

    Hope this helps

    0 讨论(0)
  • 2020-12-08 12:05

    This method no longer works in iOS 9:

    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();
    }
    
    0 讨论(0)
  • 2020-12-08 12:09

    You need to do a manual angular.bootstrap (instead of using ng-app):

     deviceReady = function() {
        angular.bootstrap(document, ['app']);
     };
     window.addEventListener('deviceready', deviceReady, false);
    
    0 讨论(0)
  • 2020-12-08 12:12

    I'm using the following solution, which allows AngularJS to be bootstrapped when running with Cordova as well as when running directly in a browser, which is where much of my development takes place. This makes sure that Angular starts up after Cordova/Phonegape is ready, and still ensures that it works when there is no Cordova/PhoneGap at all.

    // 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 run into problems with the http/https detection method, due to, perhaps, loading a Cordova app into the phone from the web, you could use the following method instead:

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

    You would still need the bootstrapAngular function from the first example, of course.

    0 讨论(0)
  • 2020-12-08 12:13

    You shouldn't use such workarounds.

    "cordova.js" isn't resolved in the browser, thus the "deviceready" event never gets fired. Just call it manually in the browser console for testing.

    var customDeviceReadyEvent = new Event('deviceready');
    document.dispatchEvent(customDeviceReadyEvent);
    
    0 讨论(0)
提交回复
热议问题