Angular:使用 & 在指令链接函数内调用控制器函数 [英] Angular: calling controller function inside a directive link function using &

查看:26
本文介绍了Angular:使用 & 在指令链接函数内调用控制器函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在尝试调用传递给指令的函数时遇到问题,该函数使用与号&"在我们指令的链接函数中.

We're running into a problem trying to call a function passed into a directive using the ampersand '&' in our directive's link function.

似乎在控制器上调用了该函数,但在调用中没有传递任何参数.我们看到的所有示例都涉及通过在模板中创建调用来传递.有没有办法从模板中调用指令上的函数,然后在指令中执行一些调用传递给它的控制器函数的操作?

It seems the function is called on the controller but no arguments are passed in the call. All the examples we have seen involve passing through by creating a call in template. Is there a way to call a function on your directive from its template, then do something in the directive that calls the controller function passed into it?

推荐答案

您是否在 {}s 中传递参数?例如,在指令的链接函数中,您需要像这样调用方法:scope.someCtrlFn({arg1: someValue});

Are you passing the arguments inside {}s? E.g., inside the directive's link function, you'll want to call the method like so: scope.someCtrlFn({arg1: someValue});

<div my-directive callback-fn="ctrlFn(arg1)"></div>

app.directive('myDirective', function() {
    return {
        scope: { someCtrlFn: '&callbackFn' },
        link: function(scope, element, attrs) {
            scope.someCtrlFn({arg1: 22});
        },
    }
});

function MyCtrl($scope) {
    $scope.ctrlFn = function(test) {
        console.log(test);
    }
}

小提琴.

这篇关于Angular:使用 &amp; 在指令链接函数内调用控制器函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆