use*_*645 19 javascript angularjs
我有外部js文件,它有更多功能.
我需要从角度控制器调用这些函数.
例如:external.js
...
...
function fun() {
...
...
}
...
...
Run Code Online (Sandbox Code Playgroud)
控制器:acccountController.js
myApp.controller('AddAccountController',function ($scope,AddAccountLoginServices,$location,localStorageService,$compile,toaster){
....
....
$scope.getLoginForm = function(siteId,name) {
...
...
fun(); // This function from external.js file
});
...
...
});
Run Code Online (Sandbox Code Playgroud)
我在acccountController.js之前导入了external.js. 但它并没有调用这个功能.而且我也没有得到任何控制台错误.
如何实现这一点...提前谢谢.
编辑:给出了错误的答案,我的不好.以下示例有效.
你的外部文件应该是这样的:
var doSomething = (function () {
"use strict";
return {
test: (function () {
return 'test';
}()),
test2: (function () {
return console.log('test 2');
})
};
}());
Run Code Online (Sandbox Code Playgroud)
并在您的控制器中调用您的脚本功能:
console.log(doSomething.test);
Run Code Online (Sandbox Code Playgroud)
要么
doSomething.test2();
Run Code Online (Sandbox Code Playgroud)
我也学到了一些东西,谢谢;)
正如@nilsK所提到的,您定义了一个自调用函数.然后通过window
对象引用它.例如 -
(function functionName(){
Do Something Here...
})();
Run Code Online (Sandbox Code Playgroud)
然后,
window.functionName();
Run Code Online (Sandbox Code Playgroud)
如果您使用的是AngularJS,
$window.functionName();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
26714 次 |
最近记录: |