角指令可以传递函数参数在该指令的属性指定前pressions? [英] Can an angular directive pass arguments to functions in expressions specified in the directive's attributes?

查看:104
本文介绍了角指令可以传递函数参数在该指令的属性指定前pressions?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用指定的回调形式指令与分离scope属性:

I have a form directive that uses a specified callback attribute with an isolate scope:

scope: { callback: '&' }

它坐落的 NG-重复所以前pression我通过在包括 ID 内该对象作为参数传递给回调函数:

It sits inside an ng-repeat so the expression I pass in includes the id of the object as an argument to the callback function:

<directive ng-repeat = "item in stuff" callback = "callback(item.id)"/>

在我的指令完成后,它会调用 $ scope.callback()从它的控制器功能。在大多数情况下,这是好的,这就是我想做的事,但有时我想从指令内添加其他参数本身。

When I've finished with the directive, it calls $scope.callback() from its controller function. For most cases this is fine, and it's all I want to do, but sometimes I'd like to add another argument from inside the directive itself.

有角的前pression,将允许这样的: $ scope.callback(ARG2),致使回调被称为与参数= [item.id,ARG2]

Is there an angular expression that would allow this: $scope.callback(arg2), resulting in callback being called with arguments = [item.id, arg2]?

如果没有,什么是做到这一点的最巧妙的方法?

If not, what is the neatest way to do this?

我发现,这个作品:

<directive 
  ng-repeat = "item in stuff" 
  callback = "callback" 
  callback-arg="item.id"/>

通过

scope { callback: '=', callbackArg: '=' }

和指令调用

$scope.callback.apply(null, [$scope.callbackArg].concat([arg2, arg3]) );

但我不认为这是特别整齐,它涉及的范围分离思想促进多余的东西。

But I don't think it's particularly neat and it involves puting extra stuff in the isolate scope.

有没有更好的办法?

Plunker操场这里(有控制台打开)。

推荐答案

如果您声明回调由@ lex82像

If you declare your callback as mentioned by @lex82 like

callback = "callback(item.id, arg2)" 

您可以调用与对象映射指令范围的回调方法,它会做正确的绑定。像

You can call the callback method in the directive scope with object map and it would do the binding correctly. Like

scope.callback({arg2:"some value"});`

无需为$解析。看到我的小提琴(控制台日志) http://jsfiddle.net/k7czc/2/

更新:有这样的<一个小例子href=\"https://docs.angularjs.org/guide/directive#creating-a-directive-that-wraps-other-elements\">documentation:

&安培;或放大器; attr指示 - 提供了一种在上下文中执行一个前pression
  父范围。如果没有指定ATTR名则属性名称
  被假设为相同的本地名称。给出的范围小部件定义:{
  localFn:'和; myAttr'},然后隔离scope属性localFn将指向
  一个函数包装为计数=计+值前pression。经常
  它是理想的通过前pression传递从隔离示波器数据
  并到母体范围,这可以通过使地图上的地方进行
  变量名和值进入前pression包装FN。 例如,
  如果EX pression是增量(量),那么我们可以指定金额
  通过调用localFn作为localFn({量:22})值。

& 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}).

这篇关于角指令可以传递函数参数在该指令的属性指定前pressions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆