角:使用与放大器调用指令链接功能的内部控制器功能; [英] Angular: calling controller function inside a directive link function using &
问题描述
我们正在运行到一个问题,试图调用传递到使用符号指令功能'和;'在我们指导的链接功能。
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);
}
}
小提琴。
这篇关于角:使用与放大器调用指令链接功能的内部控制器功能;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!