无法使用科尔多瓦插件

问题描述:

我有我想要部署为应用到移动设备的应用程序AngularJS。我听说过科尔多​​瓦(及其对应AngularJS ngCordova )。我将使用多个科尔多瓦插件。我想第一个是检测用户是否在线。要做到这一点,我使用了科尔多瓦网络-PVC门插件在通过ngCordova以下控制器:

I have an AngularJS app that I want to deploy as an app to mobile devices. I had heard about Cordova (and its AngularJS counterpart ngCordova). I will be using multiple Cordova plugins. The first one I'm trying is to detect if the user is online or not. To do that, I'm using the cordova-network-informtion plugin in the following controller via ngCordova:

'use strict';
myApp.controller('MyController', function($scope, $cordovaNetwork) {
    $scope.isOnline = null;
    $scope.init = function() {
        // Detect if the user is on a network
        console.log($cordovaNetwork.isOnline());
        $scope.isOnline = $cordovaNetwork.isOnline();
        console.log($scope.isOnline);
    };
    $scope.init();
});

在该的init 函数被调用,我在控制台中看到以下内容:

When the init function gets called, I see the following in the console:

Object {getNetwork: function, isOnline: function, isOffline: function}
TypeError {stack: (...), message: "Cannot read property 'type' of undefined"}

这类型的错误印拜全局错误处理程序。无论哪种方式,它调用的结果 $ scope.isOnline = $ cordovaNetwork.isOnline(); 。我不明白我在做什么错了。

That type error is printed thanks to global error handler. Either way, it is a result of calling $scope.isOnline = $cordovaNetwork.isOnline();. I don't understand what I'm doing wrong.

我认为根本原因是我没有以任何方式引用cordova.js。所有的例子我看到的参考cordova.js。然而,当我看包,我只看到科尔多瓦的供应商的特定实现。例如:

I believe the root cause is that I'm not referencing cordova.js in any way. All of the examples I see reference cordova.js. However, when I look at the package, I only see vendor specific implementations of Cordova. For instance:


  • cordova.windowsphone.js

  • cordova.android.js

  • cordova.ios.js

这使得从部署透视感觉。不过,我仍然试图做开发的浏览器。出于这个原因,我想我可以使用ngCordova。我还以为会有那起一个抽象的通用cordova.js。不过,我没有看到的。有人可以帮我渡过这个驼背吗?我衷心AP preciate它。

That makes sense from a deployment perspective. However, I'm still trying to do development in my browser. For that reason, I was thinking I could use ngCordova. I also thought there would be a generic cordova.js that served as an abstraction. However, I don't see one. Can someone please help me get over this hump? I sincerely appreciate it.

您的问题是,就像你在最后一段提到的,你想你的浏览器中使用插件科尔多瓦

Your problem is, like you mentioned in your last paragraph, that you are trying to use Cordova Plugins within your browser.

这是不可能,因为这里是你的浏览器不PhoneGap的抽象层。

This is not possible because here is no phonegap abstraction layer for your browser.

所以我觉得你有一些机会:

So i think you have some opportunities:

最可靠的是每一次有部署您的设备上测试。
是的,我知道,总是需要几秒钟,而不是最好的,如果你想自以为有很多不同的。

The most reliable would be to deploy it every time on your device an test it there. Yes i know, that always takes a few seconds and is not the best if you are trying a lot of different thinks.

另一个选择可能是创建自己的嘲弄 $ cordovaNetwork ngCordovaMocks

Another option could be to create your own mocked $cordovaNetwork in ngCordovaMocks.

示例:

angular.module('ngCordovaMocks', [])    
.factory('$cordovaNetwork', [function () {    
  return {    
    getNetwork: function () {
      return "Edge"
    },

    isOnline: function () {
      return true;
    },

    isOffline: function () {
      return false
    }
  }
}]);

在app.js那么你包括你 ngCordovaMocks 而不是 ngCordova 和所有服务都将被嘲笑(成为当然,只有你的嘲笑服务将可用,并且不是所有的ngCordova功能)。

In the app.js you then include your ngCordovaMocks instead of ngCordova and all services will be mocked ( Be sure, that only your mocked services will be available and not all ngCordova functionalities).

angular.module('myApp', ['ngCordovaMocks']);

是的,这是一个大量的工作,你有你的部署之前进行更改。而且不要忘了:这只是当地发展的模拟。这是必不可少的测试设备与ngCordova模块和科尔多瓦设备的连接。

Yes, this is a lot of work and you have to change it before your deployment. And don't forget: It's just a mock for local development. It's essential to test it on your device with the ngCordova module and cordova device connection.

另一种选择是使用纹波模拟器,其中提供了一些科尔多瓦在你的浏览器功能。但我不知道如果连接是一个支持。

Another option would be to use the Ripple Emulator , which offers some Cordova features in your browser. But i don't know if the connection is a supported one.

不过,那么你必须在 cordova.js 文件集成到您的index.html。

But then you have to integrate the cordova.js file to your index.html.

我不什么特定供应商的文件之间的差异,但你应该下载平原和中性 cordova.js ,包括它。

I don't what the differences between the vendor specific files are, but you should download the plain and neutral cordova.js and include it.