[英]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>
當我按下按鈕而不是打開相機時,將返回此錯誤。
提前致謝。
您需要在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>
您可以在運行塊中通過其服務注入http://ngcordova.com/docs/plugins/statusbar/ http://ngcordova.com/docs/plugins/keyboard/使用ngCordova api,因為我相信您使用的var不會不存在。 像在控制器中一樣,在運行塊中注入您使用的服務。
.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);
});
});
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.