[英]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.