角度调用控制器方法从指令与链接函数内的参数

时间:2015-11-06 20:27:41

标签: javascript angularjs angularjs-directive

我有一个指令,其链接功能类似于:

 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中的值而不是硬编码值吗?

1 个答案:

答案 0 :(得分:1)

由于您已将目标提及为@,因此您需要使用{{}}来评估该属性值,该值将被视为单向绑定。

就像你可以在你的控制器actualValue变量中有价值,然后你可以像target="{{ctrl.actualValue}}"

那样绑定它

<强>标记

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