cordova插件还没有准备好

时间:2016-06-24 15:31:24

标签: angularjs cordova ionic-framework

我有一个插件可以从DB获取信息,然后在页面上显示信息。但是当页面加载时我无法正常工作。它只适用于事件处理程序。

我试图在'deviceready'事件中引导我的应用。仍然无法工作。

请帮帮我!谢谢!

index.html文件:

<body ng-controller='loginCtl'> {{company}}
<div class="list list-inset " style="margin-top:20px;">
<button class="button button-block button-calm" ng-click="login()">Login</button>
</div> 
</body>

app.js文件:

document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady(){
    var body = document.getElementsByTagName("body")[0];
    angular.bootstrap(body,['starter']);
    //company =myplugin.getCompany(); -- doesn't work here
}

angular.module('starter', ['ionic','starter.controllers'])
.run(function($ionicPlatform) {
//......
});

angular.module('starter.controllers', [])
.controller('loginCtl', function($scope) {
    //$scope.company  = myplugin.getCompany(); --- doesn't work at here
    $scope.login=function(){
      company =myplugin.getCompany(); //work at here
      $scope.company = company;
    };            
});

1 个答案:

答案 0 :(得分:0)

感谢您的评论。我终于通过复制设备插件解决了这个问题。我的plugin.js不正确。

这是我的原始代码,仅适用于事件处理程序。

var myplugin = {
    getCompany:function() {
        exec(function(info)
         {company=info.company;},
         null, "Myplugin", "getLoginInfo", []);
        return company;
    }
};
module.exports = myplugin;

这是从cordova-plugin-device复制的,它在“deviceready”事件中起作用:

channel.createSticky('onCordovaMyReady');
// Tell cordova channel to wait on the CordovaInfoReady event
channel.waitForInitialization('onCordovaMyReady');

function Myplugin() {
    this.company = 'none';
    var me = this;
    channel.onCordovaReady.subscribe(function() {
       me.getLoginInfo(function(info) {
               me.company = info.company;
               channel.onCordovaMyReady.fire();
           },
           function(e) {                                             
               utils.alert("[ERROR] Error initializing Cordova: " + e);
           });
    });
}


Myplugin.prototype.getLoginInfo = function(successCallback, errorCallback) {
           exec(successCallback, errorCallback, "Myplugin", "getLoginInfo", []);
};

module.exports = new Myplugin();
});