I am currently developing a phonegap app using angularJS, and I have encountered an issue with a function in one of my controllers.
Here is how my controller looks like:
function NavCtrl($scope,navSvc) {
$scope.slidePage = function (path,type) {
navSvc.slidePage(path,type);
};
$scope.back = function () {
navSvc.back();
};
$scope.scan = function(){
console.log('scan(): init');
var scanner = window.cordova.require("cordova/plugin/BarcodeScanner");
scanner.scan(
function (result) {
if(result.cancelled == 1){
console.log("Scanner cancelado");
}else{
console.log("scanner ok: " + result.text);
$scope.goTo(result.text); // /products/see/result.text
}
},
function (error) {
alert("Scanning failed: " + error);
}
);
}
$scope.goTo= function(barcode){
console.log("Lets go to product " + barcode);
$scope.slidePage('/products/see/'+barcode);
}}
The first two functions handle the page transition effects. The Scan function triggers the phonegap barcode scanner plugin successfully, and the last goTo function changes the view accordingly.
The desired behavior is that when a user opens the barcode scan, they should be redirected to the corresponding product. The issue arises when, upon scanning something, the console log inside the scan function displays correctly (e.g., Scanner ok: 58746987), but the page does not change. Surprisingly, upon reopening the Barcode scan, even without scanning anything a second time, the console log ("Lets go to product XXX" where XXX is the correct barcode scanned previously) is triggered automatically, causing the goTo function to execute without another scan being performed.
This unexpected behavior has puzzled me. Despite trying to monitor variables within both the scope and the rootscope, the problem persists.
Any insights or suggestions would be greatly appreciated. Thank you in advance.