0

To describe my hurdle, A little back story. I was injecting all the requirejs path files(directives basically) which contains any directive to my main module in the app.js, which was working perfect. Its somewhat like

define(['angularAMD', 'angular-route', 'angular-resource', 'angularTranslate', 'angularTranslateLoaderStaticFiles', 'bootstrap', 'angular-idle', 'loadingMaskDirective', 'multiselectDirective', 'treeview.directive', 'tabs', 'checklist.directive'], function(angularAMD) { 
var app = angular.module("myApp", ["ngRoute", "ngResource", "ngTable", "myApp.directives", 'ui.bootstrap', 'flow']);
app.config(....){
/**some route provider**/
}...
angularAMD.bootstrap(app);
return app;

I have defined all directives and also injected them to main module. But I want to define some directive in their indivisual controller to reduce initial load. There has to be some method. Right !!

And code in my directive js files looks like..

define(['angular'], function() {
angular.module('myApp').directive('SomeDirective',
        ['$parse', '$compile', '$rootScope', '$filter', function($parse, $compile, $rootScope, $filter) {
.........

So, when I try to define the same in page controller not in app.js, It doesn't work. However I am amazed that the factory function in this case works but not directive.

Any help will be appreciated. Thanks

Gautam Kumar Samal
  • 758
  • 1
  • 7
  • 23

1 Answers1

2

Have you tried to use app to create the directive?

define(['app'], function(app) {
app.directive('SomeDirective',
        ['$parse', '$compile', '$rootScope', '$filter', function($parse, $compile, $rootScope, $filter) {
.........

An alternative provided by angularAMD is:

define(['angularAMD'], function(angularAMD) {
angularAMD.directive('SomeDirective',
        ['$parse', '$compile', '$rootScope', '$filter', function($parse, $compile, $rootScope, $filter) {
.........

The benefit of 2nd approach is to allow loading of directive before loading of your app.js. See Loading Application Wide Module for more detail.

marcoseu
  • 3,892
  • 2
  • 16
  • 35
  • Thanks @marcoseu, the first method isn't working. Tried it, though second one provides solution to my problem. So,here is angularAMD registers the directive to my main module and according to **angularAMD.<>**, can I use providers like factory,service,controller etc. in recipe section ? Thanks again for the solution. – Gautam Kumar Samal May 01 '15 at 13:16
  • We can use app.register.directive to make the first solution working. I think register exposes the providers for use. – Gautam Kumar Samal May 01 '15 at 13:21
  • @GautamKumarSamal make sure to check the version of angularAMD that you are using. The `app.register.<>` has been deprecated in favour of the ``app.<>`. – marcoseu May 01 '15 at 14:15