How to pass an argument to a method defined in the controller, but called from a directive in Angularjs?

I am trying to pass one parameter from a directive to a method defined in the controller.

I use the selection area.

Here is the corresponding code and demo in the script :

HTML

 <div ng-controller="MapCtrl">
        <map id="map_canvas" call='callMe()'></map>
    </div>

Js

var module = angular.module('googleMap', []);

module.directive('map', function() {
    return {
        restrict: 'E',
        replace: true,
        scope:{
            callMe : '&call'
        },
        template: '<div></div>',
        link: function(scope, element, attrs) {
            console.log(element);

           /* ... */                
           scope.callMe('hey');

           /* ... */                                
        }
    };
});

function MapCtrl($scope) {

    $scope.callMe = function(val){
    alert(val);
    };

}

Why am I getting val=undefined? It should behey

Thanks,

+4
source share
1 answer

You need to specify your function parameters:

Fiddle

View:

<map id="map_canvas" call='callMe(param)'></map>

Directive

scope.callMe({param: 'hey'});

This is explained in the docs (albeit somewhat vaguely):

& & attr - . attr , , . : { localFn: '& myAttr'}, scope localFn count = count + value. , , fn. , (), , localFn localFn ({amount: 22}).

+6

Source: https://habr.com/ru/post/1524029/


All Articles