在 Angular JS Controller 内部定义的函数的范围是什么?
.controller('MyCtrl', ['$scope', '$location', function($scope, $location) {
function log() {
console.log('Vanakkam Ulagam');
}
var functionToCall = 'log';
????????[functionToCall]();
}])
我尝试了 MyCtrl、this 和 $window。我可以通过像 functions.print = function()
这样的命名空间来访问该函数,然后使用 functions[functionToCall]()
。
我更感兴趣的是在不使用命名空间的情况下访问该函数。是否可以?如果不是,为什么?
最佳答案
我认为这是这个问题的解决方案
var f;
var a = angular.module('MyApp',[]);
a.controller('MyCtrl', ['$scope', function ($scope) {
$scope.log=function(){
console.log('test');
}
f=$scope.log;
});
例如,此函数可用于元素 html
<button ng-click='log()'></button>
or
<button onclick='f()'></button>
关于javascript - Angular JS Controller 内函数的作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25707671/