简体   繁体   English

如何在AngularJS Emit中将函数作为参数传递?

[英]How can you pass a function as an argument in AngularJS Emit?

I'm using the emit function in AngularJS and was wondering if it's possible to pass in a function as an argument. 我在AngularJS中使用emit函数,想知道是否可以将函数作为参数传递。

Parent Controller: 上级主管:

$scope.$on("getChildFunction", function(event, func) {
    console.log("function is...", func);
})

Child Controller: 子控制器:

$scope.$emit("getChildFunction", $scope.load_function());

However, when I use the web inspector func isn't returning at all. 但是,当我使用Web检查器时,func根本不会返回。 Are functions able to be passed up to the parent scope using emit? 是否可以使用emit将函数传递到父级作用域?

If you want to pass the function itself, omit the parenthesis: 如果要传递函数本身,请省略括号:

$scope.$emit("getChildFunction", $scope.load_function);

If you include the parenthesis - you'll execute the function and pass the result of the function. 如果包含括号-您将执行该函数并传递该函数的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM