Cordova' deviceready'事件未从角度.run块内触发 [英] Cordova 'deviceready' event not firing from within angular .run block

查看:123
本文介绍了Cordova' deviceready'事件未从角度.run块内触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从AngularJS内部进行设备就绪"注册时遇到了问题.我确定这是以前可行的,所以我不确定发生了什么变化.

I'm having issues getting 'deviceready' to register from inside of AngularJS. I'm certain this was working before, so I'm not sure what's changed.

如果我从全局addEventListener调用"deviceready",则它的工作原理如下:

If I call 'deviceready' from a global addEventListener, it works, like so:

document.addEventListener('deviceready', function(){
   localStorage.deviceReadyGlobal = true;
});

deviceReadyGlobal = true已设置.但是,如果我尝试从Angular中附加它,它将永远不会触发,就像这样:

deviceReadyGlobal=true is set. However, if I try to attach this from within Angular, it never fires, like so:

app.run(function(){
    document.addEventListener('deviceready', function(){
        localStorage.deviceReadyAngular = true;
    });
});

永远不会设置

deviceReadyAngular.现在,我了解到Angular引导时PhoneGap可能已经启动了"deviceready"功能,但是根据PhoneGap文档,这没关系.

deviceReadyAngular is never set. Now, I understand that PhoneGap probably already fired 'deviceready' while Angular was bootstrapping, but according to the PhoneGap docs, that shouldn't matter.

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

The deviceready event behaves somewhat differently from others. Any event handler registered after the deviceready event fires has its callback function called immediately.

"deviceready"的行为是否有所改变?

Did something change in the behavior of 'deviceready'?

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

I'm using Cordova 3.3.0 and Angular 1.2.5 currently.

推荐答案

这是我在应用中执行的操作;

This is how I do it inside my app;

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

这样,如果我们处于设备环境中,那么我们将侦听deviceready事件,否则,我们将忽略该事件并加载我们的应用.

This way if we are inside a device environement then we listen to deviceready event, if not, then we just ignore that event and load our app.

这篇关于Cordova' deviceready'事件未从角度.run块内触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆