使用离子框架的条形码扫描仪

时间:2015-06-11 06:34:01

标签: angularjs ionic-framework

$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);
 });

 };

这些是控制台中的错误

  

错误:[$ injector:unpr]未知提供商:   $ cordovaBarcodeScannerProvider< - $ cordovaBarcodeScanner< - AppCtrl   http://errors.angularjs.org/1.3.13/ $注射器/ unpr?P0 =%24cordovaBarcodeScannerProvider%20%3 C-%20%24cordovaBarcodeScanner%20%3 C-%20AppCtrl       在REGEX_STRING_REGEXP(ionic.bundle.js:8762)       at ionic.bundle.js:12696       at Object.getService [as get](ionic.bundle.js:12843)       at ionic.bundle.js:12701       at getService(ionic.bundle.js:12843)       在invoke(ionic.bundle.js:12875)       在Object.instantiate(ionic.bundle.js:12892)       at ionic.bundle.js:17161       在IonicModule.controller.self.appendViewElement(ionic.bundle.js:48253)       在Object.IonicModule.factory.ionicViewSwitcher.create.switcher.render   (ionic.bundle.js:46450)(匿名函数)@   ionic.bundle.js:20306 $ get @ ionic.bundle.js:17256 $ get.Scope。$ broadcast   @   ionic.bundle.js:23421 $ state.transitionTo $ state.transition.resolved.then $ state.transition。   @ ionic.bundle.js:40889processQueue @ ionic.bundle.js:21888(匿名   函数)@ ionic.bundle.js:21904 $ get.Scope。$ eval @   ionic.bundle.js:23100 $ get.Scope。$ digest @   ionic.bundle.js:22916 $ get.Scope。$ apply @ ionic.bundle.js:23205done @   ionic.bundle.js:18358completeRequest @   ionic.bundle.js:18548requestLoaded @   ionic.bundle.js:18489XMLHttpRequest.send(async)(匿名函数)@   ionic.bundle.js:18526sendReq @ ionic.bundle.js:18327 $ get.serverRequest   @ ionic.bundle.js:18043processQueue @ ionic.bundle.js:21888(匿名   函数)@ ionic.bundle.js:21904 $ get.Scope。$ eval @   ionic.bundle.js:23100 $ get.Scope。$ digest @   ionic.bundle.js:22916 $ get.Scope。$ apply @   ionic.bundle.js:23205bootstrapApply @ ionic.bundle.js:10147invoke @   ionic.bundle.js:12884doBootstrap @ ionic.bundle.js:10145bootstrap @   ionic.bundle.js:10165angularInit @ ionic.bundle.js:10059(匿名   功能)@ ionic.bundle.js:34824trigger @   ionic.bundle.js:11443eventHandler @ ionic.bundle.js:11713

2 个答案:

答案 0 :(得分:3)

似乎你想使用ngCordova包装器来实现cordova插件条形码扫描器。 ngCordova不是离子束

您是否在index.html文件中添加了ngCordova.js文件?

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

并根据您的模块

添加它
angular.module('mymodule', ['ngCordova'])

您可以使用Bower

进行安装
 bower install ngCordova

或从网站http://ngcordova.com/

下载

答案 1 :(得分:1)

确保已安装插件,从项目根目录执行:

cordova plugin add https://github.com/wildabeast/BarcodeScanner.git

您应该将$cordovaBarcodeScanner注入AppCtrl,例如:

angular.module('myapp').controller('AppCtrl', function ($scope, $cordovaBarcodeScanner) {
    $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);
            });
    };
});

另外,请确保在真实设备上进行测试,而不是ionic servecordova serve或任何模拟器。