简体   繁体   English

控制器中的Angular.js调用指令函数

[英]Angular.js call directive function in controller

I want to call a function defined in a directive, just the opposite of this stackoverflow question 我想调用指令中定义的函数,与这个stackoverflow问题恰好相反

I tried this but does not work. 我尝试了这个,但是没有用。

app.directive('myDirective', function() {
    return {
        link: function(scope, element, attrs) {
            scope.someDirectiveFn = function(arg) {
                 return "in directive";
            };
        },
    }
});

function MyCtrl($scope) {
    alert($scope.someDirectiveFn());
}

Is that possible? 那可能吗? how could I get it? 我怎么能得到它? is it a bad practice? 这是不好的做法吗?

EDIT 编辑

I got this way: 我这样子:

.controller('MyCtrl', function($scope) {
    alert($scope.func());
})

.directive('myDirective', function() {
    return {
      controller: function($scope, $element){
        $scope.func = function() {
          return "text";
         };
      }
    }
});

You can use event system to achieve that. 您可以使用事件系统来实现。

First, emit a custom event on your scope with your parameters. 首先,使用您的参数在您的范围内发出自定义事件。 Second, listen to the scope in your directive with angular $on method. 其次,使用angular $ on方法监听指令中的作用域。

app.controller('MyCtrl', function($scope) {
  $scope.invokeDirectiveMethod = function() {
    $scope.$emit('invokeMyDirectiveMethod', 'myParameter');
  };
})

.directive('myDirective', function() {
  return {
    link: function(scope, element, attrs) {
      var someDirectiveFn = function(event, arg) {
        alert(arg + " in directive");
      };

      scope.$on('invokeMyDirectiveMethod', someDirectiveFn);
    },
  }
});

Here is a working plunker . 这是一个正在工作的朋克

UPDATE UPDATE

Based on your update, event communication does not fit your problem. 根据您的更新,事件通信不适合您的问题。

How about passing an object to directive using two way binding and defining someDirectiveFn in that object? 如何通过两种方式将对象传递给指令并在该对象中定义someDirectiveFn呢? This way you can pass arguments and return values from it. 这样,您可以传递参数并从中返回值。

app.controller('MyCtrl', function($scope) {
  $scope.shareObject = {};

  $scope.invokeDirectiveMethod = function() {
    if (angular.isFunction($scope.shareObject.someDirectiveFn)) {
      $scope.message = $scope.shareObject.someDirectiveFn('from controller'); 
    }
  };
})

.directive('myDirective', function() {
  return {
    scope: {
      'shareObject': '='
    },
    link: function(scope, element, attrs) {
      scope.shareObject.someDirectiveFn = function(arg) {
        return arg + ' from parameter';
      };
    },
  }
});

Updated plunker . 更新了插件

You didnt post your html code, so I assume that your custom directive is used IN MyCtrl . 您没有发布html代码,所以我假设您的自定义指令在IN MyCtrl This does not work because of when the functions are executed. 由于执行功能时该方法不起作用。

Controller functions are always executed before link functions. 控制器功能始终在链接功能之前执行。

Here is a cool article about the differences. 是一篇有关差异的很酷的文章。

So if you want your controller to be able to invoke a function in the directive you can broadcast events (like in halilb's answer) or make the directive to listen to specific scope values, like this: 因此,如果希望控制器能够在指令中调用函数,则可以广播事件(如halilb的回答),也可以使指令监听特定的作用域值,如下所示:

app.directive('myDirective', function() {
    return {
        link: function(scope, element, attrs) {
            scope.$watch("message", function() { console.log(message); });
        },
    }
});

function MyCtrl($scope) {
    $scope.message = "Hello Directive";
}

I'm guessing you want to have a function that is shared between a directive and a controller lets say? 我猜您想拥有一个在指令和控制器之间共享的功能,可以这么说吗?

if thats the case how about creating a service and injecting the service into both directive and ctrl. 如果是这样的话,如何创建服务并将服务注入指令和ctrl中。 This way you would only have to create it once. 这样,您只需创建一次即可。

  app.service('sharedFucntionService', function() {
     return {
        someFn : function (arg) {
             alert(arg + " in directive")  
        }
    }
 });

Injecting the service into a directive 将服务注入指令

 app.directive('myDirective',function( sharedFucntionService){
  return {
        link: function (scope, element, attrs) {
            // do something with sharedFucntionService
        }
    }

}); });

Inject the service into the controller as well function MyCtrl($scope, sharedFucntionService){ ...... } 将服务以及功能MyCtrl($ scope,sharedFucntionService){......}注入控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM