Cordova'deviceready'事件没有在角度.run区域内发射

Jas*_*rth 6 javascript angularjs cordova

我在让'deviceready'从AngularJS里面注册时遇到了问题.我确定这之前有用,所以我不确定发生了什么变化.

如果我从全局addEventListener调用'deviceready',它就会起作用,如下所示:

document.addEventListener('deviceready', function(){
   localStorage.deviceReadyGlobal = true;
});
Run Code Online (Sandbox Code Playgroud)

deviceReadyGlobal = true已设置.但是,如果我尝试从Angular中附加它,它永远不会触发,如下所示:

app.run(function(){
    document.addEventListener('deviceready', function(){
        localStorage.deviceReadyAngular = true;
    });
});
Run Code Online (Sandbox Code Playgroud)

deviceReadyAngular永远不会设置.现在,我知道PhoneGap可能已经解雇了'deviceready'而Angular正在引导,但根据PhoneGap文档,这应该无关紧要.

deviceready事件的行为与其他事件略有不同.在deviceready事件触发后注册的任何事件处理程序都会立即调用其回调函数.

"deviceready"的行为有什么变化吗?

我目前正在使用Cordova 3.3.0和Angular 1.2.5.

num*_*web 5

这是我在我的应用程序中执行此操作的方式;

// Create an application module with dependencies
var app = angular.module('myApp', []);

function loadTheApp() {

    // Hide splash screen if any
    if (navigator && navigator.splashscreen) {
        navigator.splashscreen.hide();
    }

    // Initiate FastClick
    FastClick.attach(document.body);

    // Boot AngularJS
    try {
        angular.bootstrap(document, ['myApp']);
    } catch (e) {
        console.log('errrrrrrrrrrrrrr! ' + e);
    }
}

// Listen to device ready
angular.element(document).ready(function() {
    if (window.cordova) {
        document.addEventListener('deviceready', loadTheApp, false);
    } else {
        loadTheApp();
    }
});
Run Code Online (Sandbox Code Playgroud)

这样,如果我们在设备环境中,那么我们会监听deviceready事件,如果没有,那么我们只是忽略该事件并加载我们的应用程序.