简体   繁体   中英

ReferenceError: cordova is not defined @ng-cordova.min.js:7

I am new in mobile app development.For learning purpose i build a app which scan the bar code and display the text.

app.js

var app  = angular.module('starter', ['ionic','ngCordova'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {

    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
});   

app.controller('ExampleController',function($scope,$cordovaBarcodeScanner){

  $scope.scanBarcode = function () {

    $cordovaBarcodeScanner.scan().then(function (imageData) {
        alert(imageData.text);
    },function (error) {
      console.log(error);
    });
  }

});

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

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

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/services.js"></script>
    <script src="js/ng-cordova.min.js"></script>
  </head>
  <body ng-app="starter">
    <ion-panel>
      <ion-header-bar class="bar-stable">
        <h1>Ionic Blank Starter</h1>
      </ion-header-bar>
      <ion-content ng-controller="ExampleController">
        <button class="button" ng-click="scanBarcode()">Scan</button>
      </ion-content>
    </ion-panel>

  </body>
</html>

When I press the button instead of opening the camera return this error.

Thanks in advance.

You need to include ng-cordova.min.js file above the cordova.js file.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

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

    <!-- cordova script (this will be a 404 during development) -->
    <script src="js/ng-cordova.min.js"></script>
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/services.js"></script>

  </head>
  <body ng-app="starter">
    <ion-panel>
      <ion-header-bar class="bar-stable">
        <h1>Ionic Blank Starter</h1>
      </ion-header-bar>
      <ion-content ng-controller="ExampleController">
        <button class="button" ng-click="scanBarcode()">Scan</button>
      </ion-content>
    </ion-panel>

  </body>
</html>

You may use the ngCordova api through its service injection http://ngcordova.com/docs/plugins/statusbar/ http://ngcordova.com/docs/plugins/keyboard/ in the run block since I believe the var you use doesn't exist. Inject the service you use in the run block like you did in the controller.

.run(function($ionicPlatform, $cordovaKeyboard, $cordovaStatusbar) {
  $ionicPlatform.ready(function() {
    $cordovaKeyboard.hideKeyboardAccessoryBar(true);
    $cordovaKeyboard.disableScroll(true);
    $cordovaStatusbar.styleDefault();
  });
});   

And you may surround your controller scan call by $ionicPlatform.ready() since what you write in the controller can call scan low level function before it get ready.

$scope.scanBarcode = function () {
  $ionicPlatform.ready(function() {
    $cordovaBarcodeScanner.scan().then(function (imageData) {
        alert(imageData.text);
    },function (error) {
      console.log(error);
    });
  });
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM