How to pass argument to method defined in controller but called from directive in Angularjs?

前端 未结 1 1987
野的像风
野的像风 2020-12-19 15:04

I try to pass one parameter from directive to method defined in controller.

I use isolate scope.

Here is relevant code and Demo in Fiddle:

HT

相关标签:
1条回答
  • 2020-12-19 15:34

    You need to annotate your function parameters:

    FIDDLE

    View:

    <map id="map_canvas" call='callMe(param)'></map>
    

    Directive:

    scope.callMe({param: 'hey'});
    

    This is explained in the docs (although somewhat vaguely):

    & or &attr - provides a way to execute an expression in the context of the parent scope. If no attr name is specified then the attribute name is assumed to be the same as the local name. Given and widget definition of scope: { localFn:'&myAttr' }, then isolate scope property localFn will point to a function wrapper for the count = count + value expression. Often it's desirable to pass data from the isolated scope via an expression and to the parent scope, this can be done by passing a map of local variable names and values into the expression wrapper fn. For example, if the expression is increment(amount) then we can specify the amount value by calling the localFn as localFn({amount: 22}).

    0 讨论(0)
提交回复
热议问题