0

I'm trying make a dropdownlist directive, and I want it to have an attribute like onItemSelected="myCallbackFunction(selectedItem)", how can I do it? Can someone provide an example?

2 Answers 2

2

You can use the & binding (Isolate Scope Function Expression Binding)

.directive("sample", [function() {
    return {
        restrict: "A",
        scope: {
            myFunc: "&"
        },
        link: function(scope, elem, attrs) {
            scope.myFunc(); //whenever you wanna call it
        }
   }
}])

And the HTML

<div sample my-func="someScopeFunction()"></div>

Id recommend reading: https://gist.github.com/CMCDragonkai/6282750 for directive binding explanations.

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

Comments

0

To complement tymeJV's answer, if you are not using isolate scope you can make use of $eval or $parse.

app.directive('sample', function($parse){
  return {
    restrict:'A',
    link:function(scope, elem, attrs) {
      $parse(attrs.evt)(scope);

      scope.$eval(attrs.eval);
    }
  };
});

Plunk: http://plnkr.co/edit/P1hggqBXMBKBMqJtr6Zg?p=preview

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.