传递回调函数指令 [英] Pass callback function to directive

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

问题描述

我想从一个控制器指令传递一个回调函数。

I'm trying to pass a callback function from a controller to a directive.

这里的回调函数code:

Here's the callback function code:

$scope.onImageSelect = function(image) {
    alert('SET');
    $scope.card.image = image;
};

指令用法:

<google-image-search callback="onImageSelect" />

指令code:

ngmod.directive('directive', function() {
    return {
        templateUrl: '/templates/template.html',
        scope: {
            callback: '&'
        }
    }
});

在模板回调用法:

<a data-ng-click="callback(url)"></a>

然而,这给了我以下错误:

However, this gives me the following error:

TypeError: Cannot use 'in' operator to search for 'onImageSelect'

我见过很多类似的问题,但不明白的地方我错了。

I've seen a lot of similar questions, but could not understand where am I wrong.

推荐答案

在呼吁从你需要传递从该指令中的参数 JSON 格式,你也应该纠正你的指令回调属性值传递函数如回调=onImageSelect(图像)

While calling the expression method from the directive you need to pass the parameter from the directive in JSON format, also you should correct your directive callback attribute value to pass function like callback="onImageSelect(image)"

指令用法:

<google-image-search callback="onImageSelect(image)" />

指令模板

<a data-ng-click="callback({image: url})"></a>

这篇关于传递回调函数指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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