3

I'm new to AngularJS. I am writing a directive that takes in a function name (because this will change occasionally) and then I set it as the ng-click function in the template to call the function on the controller. However, it does not seem to be firing, I am sure I am doing something completely wrong so any help would be great!

I would like the fire-fn="callThis($index)" in my directive, to call the $scope.callThis on MyCtrl by getting injected into the template's html markup.

Code is here: http://plnkr.co/edit/epMfDwZILgJ5W9qIIPgr?p=preview

Thanks for the help!

1 Answer 1

10

You need to remove the '$parent' stuff that's happening in the link function, and within the directive call it with ng-click="fireFn()" instead of using {{}} there.

Check out a working version (with indices!) at http://plnkr.co/edit/VJNOyx0sBRo0QrcJLsOB?p=preview

The way '&' passes attributes is odd, from the Angular docs:

Often it's desirable to pass data from the isolated scope via an expression and to the parent scope, this can be done by passing a map of local variable names and values into the expression wrapper fn. For example, if the expression is increment(amount) then we can specify the amount value by calling the localFn as localFn({amount: 22}).

Sign up to request clarification or add additional context in comments.

Comments

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.