Search code examples
javascriptangularjsfunctioncallexternal

Call external function from AngularJS


I admit that I'm new to AngularJS and still don't know it well. However, my problem is that I need to call a function in an external .js file from a controller.

This is my code:

menuapp.controller("barcodeController", function($scope, $cordovaBarcodeScanner) {

$scope.scanBarcode = function() {
    $cordovaBarcodeScanner.scan().then(function(imageData) {
        var code = 36;//imageData.text.split('=')[1];
        if(code) {
            //external function
        }
        console.log("Barcode Format -> " + imageData.format);
        console.log("Cancelled -> " + imageData.cancelled);
    }, function(error) {
        console.log("An error happened -> " + error);
    });
};
});

I'm using this to scan a barcode, extract what's after the "=" and send it to an external function. However I can't simply call those function and I can't figure a simple way to do that.

Any help?


Solution

  • If you reference the external Javascript file from your html, you will have access to the function from within your controller.

    Html:

    <script src="URL"></script>
    

    Controller:

    if(code) {
         openrestaurant(code);
    }