简体   繁体   English

链接函数内部带有参数的指令的角度调用控制器方法

[英]Angular calling controller method from directive with parameters inside link function

I have a directive which has a link function something like: 我有一个具有链接功能的指令,例如:

 scope: {
                target: '@',
                fooFunction: '&'
            },
link:
   scope.$apply(attrs.fooFunction);
var fooValue= scope.fooFunction(data,scope.target);

in my controller i have defined this function as : 在我的控制器中,我将此功能定义为:

$scope.fooFunction= function (data, target) {
//some code here
}

When I use this in the html, I write like this: 当我在html中使用它时,我这样写:

<div some-directive  foo-Function="fooFunction(ctrl.data,'hardCoded')"  target="actualValue"></div>

where I am using myController as ctrl. 我在哪里使用myController作为ctrl。

My question is that even when I am passing the value as scope.target it always picks the hardCoded value. 我的问题是,即使我将值作为scope.target传递,它也始终会选择hardCoded值。 Is there any way I can pass the value from the scope.target instead of the hard coded value? 有什么方法可以传递scope.target中的值而不是硬编码值?

As you have mention target as @ so you need to use {{}} to evaluate that attribute value, which would be treated as one way binding. 正如您将目标称为@因此您需要使用{{}}来评估该属性值,这将被视为一种绑定方式。

Like you could have value inside your controller actualValue variable and then you could bind that like target="{{ctrl.actualValue}}" 就像您可以在控制器的actualValue变量中包含值,然后将其绑定为target="{{ctrl.actualValue}}"

Markup 标记

<div some-directive 
  foo-function="fooFunction(ctrl.data,'hardCoded')"
  target="{{ctrl.actualValue}}">
</div>

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

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