条形码扫描程序插件返回参考错误

时间:2017-02-27 05:51:43

标签: angularjs ionic-framework cordova-plugins

我试图在按钮点击时调用条形码扫描功能,但它返回以下错误:

ReferenceError: cordova is not defined
at ChildScope.$scope.scanBarcode (controllers.js:10)
at fn (eval at compile (ionic.bundle.js:27643), <anonymous>:4:224)
at ionic.bundle.js:65429
at ChildScope.$eval (ionic.bundle.js:30400)
at ChildScope.$apply (ionic.bundle.js:30500)
at HTMLButtonElement.<anonymous> (ionic.bundle.js:65428)
at defaultHandlerWrapper (ionic.bundle.js:16792)
at HTMLButtonElement.eventHandler (ionic.bundle.js:16780)
at triggerMouseEvent (ionic.bundle.js:2953)
at tapClick (ionic.bundle.js:2942)

这是我的controller.js代码:

angular.module('app.controllers', ['ionic', 'ngCordova'])
.controller('page4Ctrl', ['$scope', '$stateParams', '$cordovaBarcodeScanner',function ($scope, $stateParams, $cordovaBarcodeScanner) {
$scope.scanBarcode = function()
{
     cordova.plugins.barcodeScanner.scan().then(function(imageData) {
        alert(imageData.text);
        console.log("Barcode Format -> " + imageData.format);
        console.log("Cancelled -> " + imageData.cancelled);
    }, function(error) {
        console.log("An error happened -> " + error);
    });
};

}])

这是在调用app.js之前在index.html中插入cordova / ngcordova的代码:

<script src="lib/ionic/js/ionic.bundle.js"></script>

<script src="cordova.js"></script>
<script src="lib/ngCordova/dist/ng-cordova.js"></script>

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

您必须将条形码扫描程序的实施更改为

$scope.scanBarcode = function(){
    $cordovaBarcodeScanner.scan().then(function(imageData) {
        alert(imageData.text);
        console.log("Barcode Format -> " + imageData.format);
        console.log("Cancelled -> " + imageData.cancelled);
    }, function(error) {
        console.log("An error happened -> " + error);
    });
};

在控制器中注入了$cordovaBarcodeScannercordova.plugins.barcodeScanner将无法直接访问。

参考。 :http://ngcordova.com/docs/plugins/barcodeScanner/

问候。