How to create a directive to be used in any module?

Ghyath Serhal picture Ghyath Serhal · Nov 19, 2013 · Viewed 15.2k times · Source

How can I create a directive without linking it to a specific module which can be used in any module, such as the build-in directives.

Answer

Pasha Riger picture Pasha Riger · Nov 19, 2013

A directive or service has to belong to a module. What you can do is create separate modules for your directives and then inject them into your main application module. Here's my set up:

window.app = angular.module('app', ['ngRoute', 'app.system', 'app.animations', 'app.cart']);


angular.module('app.system', []);
angular.module('app.animations', []);
angular.module('app.cart', []);

Now your Service can be in its own module and called from within the app module. This is essentially what Ajay said.

angular.module('app.cart').factory("Cart", ['$resource', function($resource) {}]);