Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

Here's the callback function code:

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

Directive usage:

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

Directive code:

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

Callback usage in template:

<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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
218 views
Welcome To Ask or Share your Answers For Others

1 Answer

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)"

Directive usage:

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

Directive Template

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...