0
我想呼籲按鈕點擊條碼掃描功能,但它返回以下錯誤:條形碼掃描儀插件返回引用錯誤
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中插入科爾多瓦/ ngcordova代碼:
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="cordova.js"></script>
<script src="lib/ngCordova/dist/ng-cordova.js"></script>
這可能是個問題?
謝謝,我試過了,但我得到了另一個錯誤:TypeError:無法讀取屬性'掃描'的undefined –
這意味着'$ cordovaBarcodeScanner'沒有正確注入。檢查可用的ngCordova。在調用任何插件之前,還要確保設備已準備就緒。 –
加入addeventlistener工作。謝謝。 –