【发布时间】:2013-11-12 22:04:54
【问题描述】:
如何访问 javascript 插件对象并使用自定义 www/index.html 位置?
当我将此行添加到 project_name/www/config.xml 中时:
<content src="app/index.html" />
我可以为 www/index.html 加载自定义位置,但我无法访问任何已安装的 Cordova 插件。
下面的代码在加载默认的 www/index.html 时报告成功。
从www/app/index.html加载index.html时,navigator.connection未定义,报错:
'undefined' is not an object (evaluating 'navigator.connection.type')
这里是初始化phonegap的代码,等待设备准备好,初始化angularJS,然后通过插件检查连接。
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener("deviceready", this.onDeviceReady, false);
},
onDeviceReady: function() {
angular.bootstrap(document, ["myApp"]);
checkConnection();
},
}
app.initialize();
function checkConnection() {
alert('Reporting connection information');
try{
alert(navigator.connection.type);
} catch(error) {
alert('Error' + error);
alert(navigator);
}
}
附加信息:
- 此行为与 PhoneGap 3.0 和 3.1 一致
- 本机插件文件确实存在于 project_name/platforms/ios/Plugins 目录中(即:CDVConnection.m & CDVConnection.h)
- 我正在使用 AngularJS 1.2
- 通过 iOs6、iOs7 测试
- XCode 5.0 和 5.2
- 在 Phonegap 3.1 中,XCode 日志报告“2013-11-12 13:39:14.179 ESA Mobile[936:60b] 无限访问网络资源”。表明插件是在本机代码中加载的,我只是无权访问 javascript 对象。
【问题讨论】:
-
张贴您的 index.html,其中包含
cordova_plugins.js -
邮寄是什么意思? cordova_plugins.js 位于 www/cordova_plugins.js
-
我希望您从 index.html 中添加代码...所以在您的 index.html 中,您有
<script src="cordova_plugins.js">或<script src="../cordova_plugins.js">? -
cordova_plugins.js 应该包含在 www/phonegap.js 中,并且不必特别包含,尽管这是故障排除的好方向。我用the following answer 解决了这个问题。我还没有深究其背后的原因。感谢您提供帮助。
标签: ios angularjs plugins cordova