0

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?

11
  • Please show the external function you're trying to call? Commented Sep 23, 2015 at 21:43
  • Why can't you simply call the function? Commented Sep 23, 2015 at 21:46
  • @isherwood it's not getting called at all. I thought that this was normal, isn't it? Commented Sep 23, 2015 at 21:47
  • @tcooc sure, i'm editing the post Commented Sep 23, 2015 at 21:48
  • 1
    It would be more useful to see where the external is defined, rather than what it does. what it does is somewhat irrelevant. Commented Sep 23, 2015 at 21:52

2 Answers 2

2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.