[英]Angular calling controller method from directive with parameters inside link function
我有一个具有链接功能的指令,例如:
scope: {
target: '@',
fooFunction: '&'
},
link:
scope.$apply(attrs.fooFunction);
var fooValue= scope.fooFunction(data,scope.target);
在我的控制器中,我将此功能定义为:
$scope.fooFunction= function (data, target) {
//some code here
}
当我在html中使用它时,我这样写:
<div some-directive foo-Function="fooFunction(ctrl.data,'hardCoded')" target="actualValue"></div>
我在哪里使用myController作为ctrl。
我的问题是,即使我将值作为scope.target传递,它也始终会选择hardCoded值。 有什么方法可以传递scope.target中的值而不是硬编码值?
正如您将目标称为@
因此您需要使用{{}}
来评估该属性值,这将被视为一种绑定方式。
就像您可以在控制器的actualValue
变量中包含值,然后将其绑定为target="{{ctrl.actualValue}}"
标记
<div some-directive
foo-function="fooFunction(ctrl.data,'hardCoded')"
target="{{ctrl.actualValue}}">
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.