简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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