Search code examples
angularjsionic-frameworkcordova-pluginsbarcode-scanner

barcode scanner closes at once, popup doesnt show


i'm trying to show an alert box on button click using this code in controller.js:

angular.module('app.controllers', ['ionic','ngCordova'])

.controller('page1Ctrl', ['$scope', '$stateParams', '$cordovaBarcodeScanner'

function ($scope, $stateParams, $cordovaBarcodeScanner) {

document.addEventListener("deviceready", function () {

$scope.scanCode = function()
{
   $cordovaBarcodeScanner.scan(
   function (result) {
      alert("We got a barcode\n" +
            "Result: " + result.text + "\n" +
            "Format: " + result.format + "\n" +
            "Cancelled: " + result.cancelled);

  }, 
   function (error) 
   {
      alert("Scanning failed: " + error);
   }
 );
}

});

}])

but when i run the app in the device, the scanner works but the alert box displaying the details doesn't show up, and the scanner closes at once after detecting a barcode. what could be the problem?


Solution

  • Try following way as mentioned in Documentation

    $cordovaBarcodeScanner
      .scan()
      .then(function(barcodeData) {
        // Success! Barcode data is here
        alert("We got a barcode\n" +
            "Result: " + barcodeData.text + "\n" +
            "Format: " + barcodeData.format + "\n" +
            "Cancelled: " + barcodeData.cancelled);
      }, function(error) {
        // An error occurred
      });
    

    Regards.