0

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="[email protected]" 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 :)

1 Answer 1

0

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.

2
  • Your're right, I already solved the problem. However one thing I didn't know is that you don't have to specifiy the parameters to pass to controller function, you only define the name of the function. I defined it like: fn2='eventsFn(par1, par2)'. Thank you for the answer! Commented Oct 27, 2014 at 8:48
  • The effect of specifying the parameters will make a call to the function instead of just taking a reference to it. yw. Commented Oct 27, 2014 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.