如何在cordova / ionic框架中使用插件

时间:2015-02-09 08:38:34

标签: cordova ionic-framework cordova-plugins ionic

我想导入一个样本插件来测试cordova / ionic,我试试这个

$ cordova plugin add org.apache.cordova.device

现在在我的index.html中使用此代码

var model = device.model;
document.write(device.model);

然后

cd myionicApp
cordova emulate

但它不起作用,任何人都可以帮助我吗? 还安装了cordova CLI和Cordova Plugman和Ionic Framework

2 个答案:

答案 0 :(得分:1)

添加

var model = device.model;
document.write(device.model);

document.addEventListener("deviceready", success, error);
function success(){
    var model = device.model;
    document.write(device.model);
};
function error(){

};

在你的情况下它没有用,因为在设备准备好之前不会调用插件。所以我们需要添加设备就绪的监听器。然后只会调用任何插件。

问候。

答案 1 :(得分:0)

请参阅:

<html>
  <head>
    <title>Device Properties Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    //
    function onDeviceReady() {
        var element = document.getElementById('deviceProperties');
        element.innerHTML = 'Device Model: '    + device.model    + '<br />' +
                            'Device Cordova: '  + device.cordova  + '<br />' +
                            'Device Platform: ' + device.platform + '<br />' +
                            'Device UUID: '     + device.uuid     + '<br />' +
                            'Device Version: '  + device.version  + '<br />';
    }

    </script>
   </head>
   <body>
     <p id="deviceProperties">Loading device properties...</p>
   </body>
   </html>
相关问题