我在 visual studio 中有这个解决方案,我正在其中创建一个 cordova 项目。我还安装了 ngCordova 和 phonegap-plugin-barcodescanner 插件。
我已经注入(inject)了 ngCordova 并实现了这个功能:
$scope.scan = 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);
});
};
但它失败了: cordova.plugins 未定义
我错过了什么??
最佳答案
Cordova 插件只能在真实设备或模拟器上运行。
scan()
方法返回一个 promise :
$cordovaBarcodeScanner
.scan()
.then(function(barcodeData) {
// Success! Barcode data is here
console.log(barcodeData);
}, function(error) {
// An error occurred
console.log(error);
});
如果您想测试在浏览器中运行的应用程序,您可以使用这样的模拟:
if (!window.cordova){
window.cordova = {
plugins: {
barcodeScanner: {
scan: function (success, error) {
var code = window.prompt("Enter barcode value (empty value will fire the error handler):");
if(code) {
var result = {
text:code,
format:"Fake",
cancelled:false
};
success(result);
} else {
error("No barcode");
}
}
}
}
};
}
关于javascript - phonegap angularjs 条码扫描器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36993584/