FrCr
FrCr

Reputation: 263

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?

Upvotes: 0

Views: 3432

Answers (2)

Vijay Mishra
Vijay Mishra

Reputation: 39

You need to create a global instance of the function.

Add a line:

var abc= new funcName();

at the end of the File and you can use this line (var abc= new funcName();) in your controller to invoke any methods from this file by using abc.methodName.

Upvotes: 0

Tom Duggan
Tom Duggan

Reputation: 71

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);
}

Upvotes: 2

Related Questions