Javascript 在Angular js控制器中调用外部js文件函数

Javascript 在Angular js控制器中调用外部js文件函数,javascript,angularjs,Javascript,Angularjs,我有一个外部js文件,它有更多的功能 我需要从角度控制器调用这些函数 例如:external.js ... ... function fun() { ... ... } ... ... myApp.controller('AddAccountController',function ($scope,AddAccountLoginServices,$location,localStorageService,$compile,toaster){ .... .... $s

我有一个外部js文件,它有更多的功能

我需要从角度控制器调用这些函数

例如:external.js

...
...
function fun() {
  ...
  ...
}
...
...
myApp.controller('AddAccountController',function ($scope,AddAccountLoginServices,$location,localStorageService,$compile,toaster){
   ....
   ....

   $scope.getLoginForm = function(siteId,name) { 
            ...
            ...
            fun(); // This function from external.js file
   });

   ...
   ...

});
控制器:acccountController.js

...
...
function fun() {
  ...
  ...
}
...
...
myApp.controller('AddAccountController',function ($scope,AddAccountLoginServices,$location,localStorageService,$compile,toaster){
   ....
   ....

   $scope.getLoginForm = function(siteId,name) { 
            ...
            ...
            fun(); // This function from external.js file
   });

   ...
   ...

});
我在acccountController.js之前导入了external.js。但它不调用该函数。而且我也没有得到任何控制台错误


如何做到这一点。。。提前谢谢。

编辑:回答错了,我的错。下面的示例有效

您的外部文件应如下所示:

var doSomething = (function () {
  "use strict";
   return {
      test: (function () {
        return 'test';
      }()),
      test2: (function () {
        return console.log('test 2');
      })
   };
}());
在控制器中,您可以调用脚本函数:

console.log(doSomething.test);


我也学到了一些东西,谢谢;)

调用$scope.getLoginForm()的位置在哪里?。我想你从来没这么说过。所以fun()没有被调用,所以你看不到任何东西。您需要调用$scope.getLoginForm()。双向通话 在HTML中
或者在Javascript
$scope.getLoginForm()中

祝你好运

正如@nilsK所提到的,您定义了一个。然后通过
窗口
对象引用它。比如说-

(function functionName(){
    Do Something Here...
})();
然后呢,

window.functionName();
如果你使用的是AngularJS

$window.functionName();

我有类似的情况,我不必使它成为自调用函数。将外部js文件包含到html中,效果很好。

您需要创建函数的全局实例

添加一行:

var abc=new funcName();
在文件末尾,您可以在控制器中使用此行(var abc=new funcName();)通过使用abc.methodName调用此文件中的任何方法。

感谢您的回复。我是angular js的新手。如果我将var external=require('assets/plugins/formgeneration/js/external.js');在控制器的意思是,我得到这个控制台错误“ReferenceError:require未定义”。对不起,require只是node.js。。。我的错误。。。仍在努力;)如何在angular模块中导入外部js?对于angular模块,应该注入$window