Reputation: 7458
I am coming from .Net
world so I may be missing something obvious in javascript
world in angularjs
. I am writing few directives and they all have same kind of code where I need to setup same attributes.
Is it possible to create a function and use that function inside the link function of angular?
Thanks
Upvotes: 1
Views: 422
Reputation: 26841
Ofcourse, this is not something hard to do with javascript.
Here is some example code that does that.
(function(){
function commonCode(){
//common code goes here.
}
var app = angular.module("app");
app.directive("first",function(){
return function(scope,element,attrs){
commonCode();
}
});
app.directive("second",function(){
return function(scope,element,attrs){
commonCode();
}
});
app.directive("third",function(){
return function(scope,element,attrs){
commonCode();
}
});
})();
And its even easier to do it with Angularjs.
If this common code is extremely generic, then you could refactor out this commonCode
into a service
and inject
it into all your directives
.
Upvotes: 1