How to call a method defined in an AngularJS directive?

前端 未结 13 1085
萌比男神i
萌比男神i 2020-11-22 14:39

I have a directive, here is the code :

.directive(\'map\', function() {
    return {
        restrict: \'E\',
        replace: true,
        template: \'<         


        
13条回答
  •  抹茶落季
    2020-11-22 15:32

    Just use scope.$parent to associate function called to directive function

    angular.module('myApp', [])
    .controller('MyCtrl',['$scope',function($scope) {
    
    }])
    .directive('mydirective',function(){
     function link(scope, el, attr){
       //use scope.$parent to associate the function called to directive function
       scope.$parent.myfunction = function directivefunction(parameter){
         //do something
    }
    }
    return {
            link: link,
            restrict: 'E'   
          };
    });
    

    in HTML

提交回复
热议问题