执行带有参数的范围内绑定功能 [英] Executing a scope bound function with parameters

查看:85
本文介绍了执行带有参数的范围内绑定功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的功能的指令:

 范围:{
      loadChildren:与&
 },

我怎样才能把这个从我的控制器和传递参数?

  $ scope.loadChildren(参数1,参数2);

我的指令执行我通过它的功能,但我没有看到任何参数。

有没有办法通过他们这样?

编辑回复:rodyhaddad的答案

rodyhaddad点的方式,并给出了参数传递给绑定功能的正确格式。然而,为了得到它的工作我也只好参数键添加到我的HTML是这样的:

 <例负载孩子=负载(参数1,参数2)>< /示例>

然后我的控制器上,我可以看到参数:

  $ scope.load =功能(参数1,参数2){
    调试器;
};


解决方案

当你做

loadChildren:&放大器;

这是code,它生成功能(的来源$ C ​​$ C

情况下'和;':{
  parentGet = $解析(ATTRS [attrName]);
  范围[scopeName] =功能(当地人){
    返回parentGet(parentScope,当地人);
  };
  打破;

因此​​,大家可以看到,无论你传递给 loadChildren 将成为当地人传递给 $解析

所以,正确的方法来调用这将是:

$ scope.loadChildren({
  参数1:参数1,
  参数2:参数2
});

如果你想要一个例子,这里是如何活动的指令(如 ngClick )暴露 $事件来的前pressions:源$ C ​​$ C

If I have a function like this in a directive:

 scope: {
      loadChildren: "&"
 },

How can I call this from my controller and pass parameters?

$scope.loadChildren(param1, param2);

My directive executes the function I pass it but I don't see any parameters.

Is there any way to pass them like this?

EDIT re: rodyhaddad's answer

rodyhaddad points the way and gives the correct format for passing parameters to a bound function. However, in order to get it working I also had to add the parameter keys into my HTML like this:

<example load-children="load(param1, param2)"></example>

Then on my controller I can see parameters:

$scope.load = function (param1, param2) {
    debugger;
};

解决方案

When you do

  loadChildren: "&"

This is the code that generates the function (source code)

case '&': {
  parentGet = $parse(attrs[attrName]);
  scope[scopeName] = function(locals) {
    return parentGet(parentScope, locals);
  };
  break;

So as you can see, whatever you pass to loadChildren will become the locals passed to $parse.

So the correct way to call it would be:

$scope.loadChildren({
  param1: param1,
  param2: param2
});

If you want another example, here's how event directives (like ngClick) expose $event to their expressions: source code

这篇关于执行带有参数的范围内绑定功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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