5

I'm having two directives with same functionality like below.

angular.module('ui.directives', [])
  .directive('uiFoo', 
    function() {
      return {
        restrict: 'EAC',
        link: function($scope, element, attrs) {
          //to do functionality
          element.append("test content");
        }
       };
    })
  .directive('uiFoo1', 
    function() {
      return {
        restrict: 'EAC',
        link: function($scope, element, attrs) {
          //to do functionality
          element.append("test content");
        }
      };
    });

They both contain the same working like here it is appending "test content" as text to that element. Is there any chance that instead making this two directive. Can i write two names for one directive/ I can use same functionality in with optimize code. Here I'm writing same code for without any meaning. Instead of writing directive two times, is there any optimize way. I'm new in AngularJS kindly help me. Thanks in advance!

6
  • 1
    Why can't you doit in the same directive making it more configurable? Commented Aug 20, 2014 at 18:47
  • I'm confused, do you want both directives to do exactly the same? Commented Aug 20, 2014 at 18:48
  • @PSL i want to apply directive for input and select element.and both wil have same functionality..then how should achieve it? Commented Aug 20, 2014 at 18:50
  • You don't need them to have different names to do that. Commented Aug 20, 2014 at 18:52
  • @pankajparkar You can do away with just one directive. What exactly are you doing in the directive(s)? Commented Aug 20, 2014 at 18:55

1 Answer 1

7

The simpliest way would be to extract your directive into a JS object and use this instead.

Alternativly you can provide the directive object with a angular provider, if you want to stay in the angular context.

But, why do you want to have two directives with the exact same functionality in the first place?
Directives can used as often as you want, so this seems like a design flaw to me.

var myDirective = [function(){
            restrict: 'EAC',
            link: function($scope, element, attrs) {
              //to do functionality
              element.append("test content");
            }
          }];

angular.module('ui.directives', [])
      .directive('uiFoo', myDirective)
      .directive('uiFoo1', myDirective);
Sign up to request clarification or add additional context in comments.

2 Comments

Why not set myDirective = function()... Then, if you need to inject dependencies, you only have to do it once. Note that to be minifiable, you would return an array eg. myDirective = ['$http', function($http)...]
Of course you can extract the whole function too. I'll change the answer accordingly.

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.