我尝试了一个非常简单的例子:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Hello World</title>
</head>
<body data-ng-app="MyApp">
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
<script type="text/javascript" src="js/libs/cordova.js"></script>
<script type="text/javascript" src="js/libs/angular.js"></script>
<script type="text/javascript" src="js/modules/app.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>
</html>
你可以看到我包括cordova.js
,angular.js
和模块app.js
。
您可以使用依赖注入向模块添加角度服务,所以我
尝试了以下内容:
var app = angular.module('MyApp', []);
app.run(function($document) {
$document.on('deviceready' function() {
alert("deviceready");
});
});
不幸的是,警报从未被调用过。这意味着&#39; deviceready&#39;的回调方法从来没有被称为注射似乎失败。
所以我要问上面的代码有什么问题?
修改
启动应用程序时整个日志输出:
04-18 17:55:27.430: I/CordovaLog(13660): Changing log level to DEBUG(3)
04-18 17:55:27.430: D/CordovaActivity(13660): CordovaActivity.init()
04-18 17:55:27.476: I/WebViewFactory(13660): Loading com.google.android.webview version 40 (1832189-arm) (code 424501)
04-18 17:55:27.510: I/LibraryLoader(13660): Time to load native libraries: 8 ms (timestamps 1958-1966)
04-18 17:55:27.511: I/LibraryLoader(13660): Expected native library version number "",actual native library version number ""
04-18 17:55:27.533: V/WebViewChromiumFactoryProvider(13660): Binding Chromium to main looper Looper (main, tid 1) {3d68a6e5}
04-18 17:55:27.533: I/LibraryLoader(13660): Expected native library version number "",actual native library version number ""
04-18 17:55:27.534: I/chromium(13660): [INFO:library_loader_hooks.cc(108)] Chromium logging enabled: level = 0, default verbosity = 0
04-18 17:55:27.542: I/BrowserStartupController(13660): Initializing chromium process, singleProcess=true
04-18 17:55:27.547: W/art(13660): Attempt to remove local handle scope entry from IRT, ignoring
04-18 17:55:27.568: W/chromium(13660): [WARNING:resource_bundle.cc(304)] locale_file_path.empty()
04-18 17:55:27.569: I/chromium(13660): [INFO:aw_browser_main_parts.cc(63)] Load from apk succesful, fd=31 off=35788 len=3738
04-18 17:55:27.570: I/chromium(13660): [INFO:aw_browser_main_parts.cc(76)] Loading webviewchromium.pak from, fd:32 off:390788 len:1143511
04-18 17:55:27.574: W/AudioManagerAndroid(13660): Requires BLUETOOTH permission
04-18 17:55:27.581: I/Adreno-EGL(13660): <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 01/15/15, ab0075f, Id3510ff6dc
04-18 17:55:27.714: W/chromium(13660): [WARNING:data_reduction_proxy_settings.cc(328)] SPDY proxy OFF at startup
04-18 17:55:27.748: W/art(13660): Attempt to remove local handle scope entry from IRT, ignoring
04-18 17:55:27.759: W/AwContents(13660): onDetachedFromWindow called when already detached. Ignoring
04-18 17:55:27.760: D/CordovaActivity(13660): CordovaActivity.createViews()
04-18 17:55:27.791: D/PluginManager(13660): init()
04-18 17:55:27.799: D/CordovaWebView(13660): CordovaWebView is running on device made by: LGE
04-18 17:55:27.811: W/art(13660): Attempt to remove local handle scope entry from IRT, ignoring
04-18 17:55:27.812: W/art(13660): Attempt to remove local handle scope entry from IRT, ignoring
04-18 17:55:27.812: D/CordovaWebView(13660): >>> loadUrl(file:///android_asset/www/index.html)
04-18 17:55:27.812: D/CordovaWebView(13660): >>> loadUrlNow()
04-18 17:55:27.831: D/CordovaActivity(13660): Resuming the App
04-18 17:55:27.840: D/OpenGLRenderer(13660): Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-18 17:55:27.851: D/Atlas(13660): Validating map...
04-18 17:55:27.895: D/SoftKeyboardDetect(13660): Ignore this event
04-18 17:55:27.908: I/OpenGLRenderer(13660): Initialized EGL, version 1.4
04-18 17:55:27.914: D/OpenGLRenderer(13660): Enabling debug mode 0
04-18 17:55:28.094: D/SoftKeyboardDetect(13660): Ignore this event
04-18 17:55:28.131: D/CordovaWebViewClient(13660): onPageStarted(file:///android_asset/www/index.html)
04-18 17:55:28.131: D/CordovaActivity(13660): onMessage(onPageStarted,file:///android_asset/www/index.html)
04-18 17:55:28.160: W/BindingManager(13660): Cannot call determinedVisibility() - never saw a connection for the pid: 13660
04-18 17:55:28.192: D/CordovaLog(13660): file:///android_asset/www/index.html: Line 7 : The key "target-densitydpi" is not supported.
04-18 17:55:28.193: I/chromium(13660): [INFO:CONSOLE(7)] "The key "target-densitydpi" is not supported.", source: file:///android_asset/www/index.html (7)
04-18 17:55:28.294: D/JsMessageQueue(13660): Set native->JS mode to OnlineEventsBridgeMode
04-18 17:55:28.367: D/CordovaActivity(13660): onMessage(spinner,stop)
04-18 17:55:28.595: D/CordovaLog(13660): file:///android_asset/www/js/modules/app.js: Line 10 : Uncaught SyntaxError: Unexpected token function
04-18 17:55:28.596: I/chromium(13660): [INFO:CONSOLE(10)] "Uncaught SyntaxError: Unexpected token function", source: file:///android_asset/www/js/modules/app.js (10)
04-18 17:55:28.598: D/CordovaLog(13660): file:///android_asset/www/index.html: Line 16 : Uncaught ReferenceError: app is not defined
04-18 17:55:28.598: I/chromium(13660): [INFO:CONSOLE(16)] "Uncaught ReferenceError: app is not defined", source: file:///android_asset/www/index.html (16)
04-18 17:55:28.769: D/CordovaLog(13660): file:///android_asset/www/js/libs/angular.js: Line 4138 : Uncaught Error: [$injector:modulerr] Failed to instantiate module MyApp due to:
04-18 17:55:28.769: D/CordovaLog(13660): Error: [$injector:nomod] Module 'MyApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
04-18 17:55:28.769: D/CordovaLog(13660): http://errors.angularjs.org/1.3.15/$injector/nomod?p0=MyApp
04-18 17:55:28.769: D/CordovaLog(13660): at file:///android_asset/www/js/libs/angular.js:63:12
04-18 17:55:28.769: D/CordovaLog(13660): at file:///android_asset/www/js/libs/angular.js:1774:17
04-18 17:55:28.769: D/CordovaLog(13660): at ensure (file:///android_asset/www/js/libs/angular.js:1698:38)
04-18 17:55:28.769: D/CordovaLog(13660): at module (file:///android_asset/www/js/libs/angular.js:1772:14)
04-18 17:55:28.769: D/CordovaLog(13660): at file:///android_asset/www/js/libs/angular.js:4115:22
04-18 17:55:28.769: D/CordovaLog(13660): at forEach (file:///android_asset/www/js/libs/angular.js:323:20)
04-18 17:55:28.769: D/CordovaLog(13660): at loadModules (file:///android_asset/www/js/libs/angular.js:4099:5)
04-18 17:55:28.769: D/CordovaLog(13660): at createInjector (file:///android_asset/www/js/libs/angular.js:4025:11)
04-18 17:55:28.769: D/CordovaLog(13660): at doBootstrap (file:///android_asset/www/js/libs/angular.js:1452:20)
04-18 17:55:28.769: D/CordovaLog(13660): at bootstrap (file:///android_asset/www/js/libs/angular.js:1473:12)
04-18 17:55:28.769: D/CordovaLog(13660): http://errors.angularjs.org/1.3.15/$injector/modulerr?p0=MyApp&p1=Error%3A%20%5B%24injector%3Anomod%5D%20Module%20'MyApp'%20is%20not%20available!%20You%20either%20misspelled%20the%20module%20name%20or%20forgot%20to%20load%20it.%20If%20registering%20a%20module%20ensure%20that%20you%20specify%20the%20dependencies%20as%20the%20second%20argument.%0Ahttp%3A%2F%2Ferrors.angularjs.org%2F1.3.15%2F%24injector%2Fnomod%3Fp0%3DMyApp%0A%20%20%20%20at%20file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A63%3A12%0A%20%20%20%20at%20file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A1774%3A17%0A%20%20%20%20at%20ensure%20(file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A1698%3A38)%0A%20%20%20%20at%20module%20(file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A1772%3A14)%0A%20%20%20%20at%20file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A4115%3A22%0A%20%20%20%20at%20forEach%20(file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A323%3A20)%0A%20%20%20%20at%20loadModules%20(file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A4099%3A5)%0A%20%20%20%20at%20createInjector%20(file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A4025%3A11)%0A%20%20%20%20at%20doBootstrap%20(file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A1452%3A20)%0A%20%20%20%20at%20bootstrap%20(file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A1473%3A12)
04-18 17:55:28.770: I/chromium(13660): [INFO:CONSOLE(4138)] "Uncaught Error: [$injector:modulerr] Failed to instantiate module MyApp due to:
04-18 17:55:28.770: I/chromium(13660): Error: [$injector:nomod] Module 'MyApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
04-18 17:55:28.770: I/chromium(13660): http://errors.angularjs.org/1.3.15/$injector/nomod?p0=MyApp
04-18 17:55:28.770: I/chromium(13660): at file:///android_asset/www/js/libs/angular.js:63:12
04-18 17:55:28.770: I/chromium(13660): at file:///android_asset/www/js/libs/angular.js:1774:17
04-18 17:55:28.770: I/chromium(13660): at ensure (file:///android_asset/www/js/libs/angular.js:1698:38)
04-18 17:55:28.770: I/chromium(13660): at module (file:///android_asset/www/js/libs/angular.js:1772:14)
04-18 17:55:28.770: I/chromium(13660): at file:///android_asset/www/js/libs/angular.js:4115:22
04-18 17:55:28.770: I/chromium(13660): at forEach (file:///android_asset/www/js/libs/angular.js:323:20)
04-18 17:55:28.770: I/chromium(13660): at loadModules (file:///android_asset/www/js/libs/angular.js:4099:5)
04-18 17:55:28.770: I/chromium(13660): at createInjector (file:///android_asset/www/js/libs/angular.js:4025:11)
04-18 17:55:28.770: I/chromium(13660): at doBootstrap (file:///android_asset/www/js/libs/angular.js:1452:20)
04-18 17:55:28.770: I/chromium(13660): at bootstrap (file:///android_asset/www/js/libs/angular.js:1473:12)
04-18 17:55:28.770: I/chromium(13660): http://errors.angularjs.org/1.3.15/$injector/modulerr?p0=MyApp&p1=Error%3A%20%5B%24injector%3Anomod%5D%20Module%20'MyApp'%20is%20not%20available!%20You%20either%20misspelled%20the%20module%20name%20or%20forgot%20to%20load%20it.%20If%20registering%20a%20module%20ensure%20that%20you%20specify%20the%20dependencies%20as%20the%20second%20argument.%0Ahttp%3A%2F%2Ferrors.angularjs.org%2F1.3.15%2F%24injector%2Fnomod%3Fp0%3DMyApp%0A%20%20%20%20at%20file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A63%3A12%0A%20%20%20%20at%20file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A1774%3A17%0A%20%20%20%20at%20ensure%20(file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A1698%3A38)%0A%20%20%20%20at%20module%20(file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A1772%3A14)%0A%20%20%20%20at%20file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A4115%3A22%0A%20%20%20%20at%20forEach%20(file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A323%3A20)%0A%20%20%20%20at%20loadModules%20(file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A4099%3A5)%0A%20%20%20%20at%20createInjector%20(file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A4025%3A11)%0A%20%20%20%20at%20doBootstrap%20(file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A1452%3A20)%0A%20%20%20%20at%20bootstrap%20(file%3A%2F%2F%2Fandroid_asset%2Fwww%2Fjs%2Flibs%2Fangular.js%3A1473%3A12)", source: file:///android_asset/www/js/libs/angular.js (4138)
04-18 17:55:28.774: D/CordovaWebViewClient(13660): onPageFinished(file:///android_asset/www/index.html)
04-18 17:55:28.774: D/CordovaActivity(13660): onMessage(onPageFinished,file:///android_asset/www/index.html)
04-18 17:56:03.805: D/CordovaActivity(13660): Paused the application!
04-18 17:56:03.806: D/CordovaWebView(13660): Handle the pause
答案 0 :(得分:6)
您的app.js文件中有拼写错误
var app = angular.module('MyApp', []);
app.run(function($document) {
// was missing comma here \/
$document.on('deviceready', function() {
alert("deviceready");
});
});