2016-05-18 189 views
0

我是移动应用程序开发的新手。为了学习目的,我构建了一个扫描条形码并显示文本的应用程序。ReferenceError:cordova is not defined @ ng-cordova.min.js:7

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> 

当我按下按钮,而不是打开相机返回该错误的。

在此先感谢。

回答

0

,因为我相信你使用var不存在,您可以通过在运行程序段的服务注入 http://ngcordova.com/docs/plugins/statusbar/ http://ngcordova.com/docs/plugins/keyboard/ 使用ngCordova API。 像在控制器中那样注入您在运行块中使用的服务。

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

你可以围绕由$ ionicPlatform.ready()控制器扫描呼叫,因为你在控制器中写什么可以调用扫描低级别的功能之前,做好准备。

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

,如果你提供一些代码示例,将是有益的 –

+0

为什么我们需要在这里cordovakeyboard插件 –

+0

问题依然存在:( –

1

您需要在cordova.js文件上方包含ng-cordova.min.js文件。

<!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> 
相关问题