简体   繁体   English

AngularJS:从嵌套指令执行控制器功能

[英]AngularJS: Execute controller function from a nested directive

I have a question regarding executing a function (defined inside a controller) from a directive, but with another directive between these two (so this directive just passes forward the function). 我有一个关于从指令执行功能(在控制器内部定义)的问题,但是在这两个指令之间有另一个指令(因此,该指令只是将功能向前传递)。 I made a quick working example, but I think it's not the best solution to my problem: 我做了一个快速的示例,但是我认为这不是解决我的问题的最佳方法:

http://plnkr.co/edit/mS8Oea?p=preview http://plnkr.co/edit/mS8Oea?p=preview

Here's the code: 这是代码:

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.0.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js" data-semver="1.0.8"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">

    <div agg-events="" fn="ctrlFn(par1, par2)"></div>

  </body>

</html>

If someone knows how to do this in a better way, it would be very helpful to me. 如果有人知道如何更好地做到这一点,那对我很有帮助。 Thanks :) 谢谢 :)

I guess you have already solved your problem but I fixed your plunker here: plunkr 我想您已经解决了您的问题,但是我在这里固定了您的松紧琴: plunkr

The basis is to assign the function to your attribute not the evaluated function eg. 基础是将功能分配给您的属性,而不是评估的功能,例如。

Do fn="ctrFn" and call the function in your directive. 执行fn="ctrFn"并在指令中调用该函数。

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

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