从内部指令将参数传递给 ngChange 事件调用 [英] Passing arguments to ngChange event call from inside directive

查看:23
本文介绍了从内部指令将参数传递给 ngChange 事件调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受 ng-change 属性的指令:

I have a directive that accepts an ng-change attribute:

<radio-buttons options="optionsList" 
               ng-model="myModel" 
               ng-change="myCallback($event)"></radio-buttons>

我在控制器中定义了一个函数,myCallback,如下所示:

I've defined a function in my controller, myCallback, that looks like this:

$scope.myCallback = function(e) {
    console.log("Callback from controller");   
    console.log(e);
}

以下函数 select 存在于我的 radioButton 指令中.我需要在 select 函数中的指令中定义何时执行 ngChange 回调:

The following function select exists within my radioButton directive. I need to define when the ngChange callback is executed inside my directive in the select function:

function select(scope, val) {
    if (!scope.disabled && scope.selectedValue != val) {
        scope.selectedValue = val;
        scope.model = val;

        scope.callback.call();
    }
}

我遇到的问题是当我在 中执行 myCallback 时,myCallback 中的参数 $event 没有被传递选择我的指令的功能.

The problem I am having is the argument $event in myCallback is not getting passed along when I execute myCallback inside the select function of my directive.

小提琴:http://jsfiddle.net/dkrotts/BtrZH/7/ 更新:http://jsfiddle.net/dkrotts/BtrZH/8/

我做错了什么?

推荐答案

你必须像这样在回调中传递参数:

You have to pass the parameter in the callback like so:

callback({parametername: value});

并且您必须将参数名称与 HTML

And you have to match the parameter name with the one declared in the HTML

在你的情况下:

callback({$event: val})

这篇关于从内部指令将参数传递给 ngChange 事件调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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