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
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.
Comments
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);
}
};
});