1

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

1 Answer 1

1

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.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.