Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing directives for multiple modules

I would like to know what is the best way to reuse the same directive for multiple modules. In my mind i think directives are like DOM-modules, so the architecture of angular for this is a bit annoying of my point of view.

The way I'm doing it now is:

var mainapp = angular.module('MainApp');
mainapp.controller( ... );

var otherapp = angular.module('OtherApp');
otherapp.controller( ... );

And in other file i have my directive i want to use in this two controllers/modules

mainapp.directive('autoselect', function(){ ... });

But as you can see, I have to specify in what app I have to use it. If I want to use in both, do i have to copy twice the same code?

like image 403
EnZo Avatar asked Dec 26 '22 02:12

EnZo


1 Answers

You can maintain a module that contains common (services, directives, values, constants etc.) and inject into each module.

angular.module('CommonApp',['OtherCommonModule1','OtherCommonModule2']).directive(..);

var mainapp = angular.module('MainApp', ['CommonApp']);
mainapp.controller( ... );

var otherapp = angular.module('OtherApp', ['CommonApp']);
otherapp.controller( ... );
like image 104
Dylan Avatar answered Dec 28 '22 07:12

Dylan