0

I'm working on a a large scale angular project with a team of devs.

the problem we run into is if you have several files for a component, say a directive.

some-directive.js
some-directive-controller.js

in the definition of both files you would have to attach them to a module, however one file must create the module with []. If the developer forgets to poke around they will add [] in the second file called will actually overwrite the module. so now it becomes a memory game. Each developer has to remember to only declare the module in one file []

some-directive.js

angular.module('some-module',['some-dependencies']).directive('some-directive',function(){});

some-controller.js

angular.module('some-module',[]).controller('some-controller',function(){});

we have been using the following approach. Is there a better way?

some-directive.js
some-directive-module.js
some-directive-controller.js

where some-directive-module only contains the module creation, includes any dependencies, and does any .config needed. Still the dev needs to remember to angular.module('some-directive') in all the other files without the square brackets.

some-directive-module.js

angular.module('some-directive',[])
.config(//someconfig stuff);

some-directive-module.js

angular.module('some-directive).directive(//declare directive);

some-directive-controller.js

angular.module('some-directive).controller(//declare contrller used by directive);

I suggested that instead we should do the following, it eliminates the issue of overwriting modules, but I received some negative feedback from one of the other devs

some-directive-module.js

angular.module('some-directive',['some-directive.directive','some-directive.controller'])
.config(//someconfig stuff);

some-directive-module.js

angular.module('some-directive.directive',[]).directive(//declare directive);

some-directive-controller.js

angular.module('some-directive.controller',[]).controller(//declare contrller used by directive);

Is there a better way? Or is one of the above options correct?

1
  • I recommend you to take a look at the John Papa's Angular Styleguide github.com/johnpapa/angular-styleguide. It goes beyond a pattern for naming stuff and gives you a whole world of good practices to Angular apps. It is also flexible so you can adapt it to your own preferences. I'm using it on my projects and everything is going fine. Commented Jun 17, 2015 at 13:36

1 Answer 1

1

The recommended way (by multiple competent people) is to use the setter-getter-syntax (creating once with angular.module("someModule",[]) and accessing with angular.module("someModule") from there on). Putting the module definition and configuration into one file seems very clean and is common practice between a lot of developers. But make sure not to create a module for every single directive - group services, directives, constants and so on into reasonable functionality-modules instead.

Making clear what a file contains by its name is also a good idea in my opinion, so your some-directive-module.js approach seems fine to me. If developers "poke around" and "wildly add []", they should get a slap on the wrist follwoed by an explanation how modules work in angular, so they stop doing it ;-)

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.