javascript - 从 AngularJS 调用外部函数

标签 javascript angularjs function call external

我承认我是 AngularJS 的新手,但仍然不太了解它。 但是,我的问题是我需要从 Controller 调用外部 .js 文件中的函数。

这是我的代码:

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

我用它来扫描条形码,提取“=”后面的内容并将其发送到外部函数。但是我不能简单地调用这些函数,而且我也想不出一种简单的方法来做到这一点。

有什么帮助吗?

最佳答案

如果您从 html 引用外部 Javascript 文件,您将可以从 Controller 内访问该函数。

HTML:

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

Controller :

if(code) {
     openrestaurant(code);
}

关于javascript - 从 AngularJS 调用外部函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32750004/

相关文章:

javascript - yyyy-mm-dd 的正则表达式

javascript - 工厂中的 AngularJS $http 请求

java - 在 Java 中使用 for 循环打印特定模式

javascript - 如何创建一个具有多维数组输入、转换它并输出新的多维数组的函数

javascript - 是否有可能从 javascript 中的闭包外部调用 f1() 函数

javascript - AngularJS + 单核 CPU = 几乎 100% CPU

javascript - URL 正则表达式改进以允许本地主机 URL

javascript - 检查动态文本字段Angularjs的输入字段是否为空

angularjs - 在 $rootScope 上运行 $apply 与任何其他范围

javascript - Angular 选择延迟加载项目的默认值