我有一个功能更多的外部js文件。
我需要从angular控制器调用这些函数。
例如:external.js
...
...
function fun() {
...
...
}
...
...
控制器:acccountController.js
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。但是它不会调用那个函数。而且我也没有收到任何关于这个的控制台错误。
如何实现这一点...提前谢谢。
发布于 2014-05-16 22:19:33
编辑:回答错误,是我的错。下面的示例起作用。
您的外部文件应如下所示:
var doSomething = (function () {
"use strict";
return {
test: (function () {
return 'test';
}()),
test2: (function () {
return console.log('test 2');
})
};
}());
在你的控制器中调用你的脚本函数:
console.log(doSomething.test);
或
doSomething.test2();
我也学到了一些东西,谢谢;)
发布于 2015-06-28 17:41:25
正如@nilsK所提到的,您可以定义一个self invoking function。然后通过window
对象引用它。例如:
(function functionName(){
Do Something Here...
})();
然后,
window.functionName();
如果您使用的是AngularJS,
$window.functionName();
发布于 2015-10-04 22:34:12
我有类似的情况,我不需要让它自己调用函数。将外部js文件包含到html中,它工作得很好。
https://stackoverflow.com/questions/23697493
复制相似问题