How to call a method defined in an AngularJS directive?

前端 未结 13 1071
萌比男神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:35

    Below solution will be useful when, you are having controllers (both parent and directive (isolated)) in 'controller As' format

    someone might find this useful,

    directive :

    var directive = {
            link: link,
            restrict: 'E',
            replace: true,
            scope: {
                clearFilters: '='
            },
            templateUrl: "/temp.html",
            bindToController: true, 
            controller: ProjectCustomAttributesController,
            controllerAs: 'vmd'
        };
        return directive;
    
        function link(scope, element, attrs) {
            scope.vmd.clearFilters = scope.vmd.SetFitlersToDefaultValue;
        }
    }
    

    directive Controller :

    function DirectiveController($location, dbConnection, uiUtility) {
      vmd.SetFitlersToDefaultValue = SetFitlersToDefaultValue;
    
    function SetFitlersToDefaultValue() {
               //your logic
            }
    }
    

    html code :

          <Test-directive clear-filters="vm.ClearFilters"></Test-directive>
        <a class="pull-right" style="cursor: pointer" ng-click="vm.ClearFilters()"><u>Clear</u></a> 
    //this button is from parent controller which will call directive controller function
    
    0 讨论(0)
提交回复
热议问题