0

I'm a web developer who mostly works on my own. When building apps I have complete freedom regarding how I structure my code. Nonetheless, I try to conform to industry conventions as much as possible, since this makes life easier for other developers who may need to maintain my code down the line.

I've just started working with Angularjs. Since angular is relatively new, I wonder if any conventions have emerged yet regarding how projects should be structured.

Specifically, I've noticed that Yeoman creates this setup by default:

app
 |-bower_components
 |-images
 |-scripts
 |---controllers
 |------ *.js
 |-styles
 |--- *.css
 |-views
 |--- *.html

Two questions:

  1. Is this structure widely used? (i.e. should I be using it too?)
  2. Where should I place directives that I write? In app/scripts/directives?
3
  • 1
    Take also a look on Mastering Web Application Development with AngularJS deploy it's separates folders by feature and above all has all the feature view in the same folder named .tpl doing so you can exploit it with htmlTojs grunt tool
    – Whisher
    Commented Mar 21, 2014 at 19:15
  • 1
    I use a structure similar to @rom99 below, in my code has 'Modules' (which is the directory name under /app/) and in their each module is a folder, with all the related files. I include the tests as well. /app/Modules/Users/controllers, /app/Modules/Users/views, etc... and then /app/Modules/Scheduling/controllers, /app/Modules/Customer/controllers, etc... Commented Mar 21, 2014 at 19:22
  • Agree with both comments here and answer below. I tend to create my modules for a given feature set that includes the services, controllers, and possibly directives (usually give them their own module based on types of directives). This way if in the future angular allows or you find your own way to lazy load you can get just the features needed for a given view. I generally use yeoman with the angular-generator to start then tweak the structure from there. Commented Mar 21, 2014 at 19:48

2 Answers 2

3

There seem to be a couple of conventions I've seen people use, there's the Angular seed project (https://github.com/angular/angular-seed) which I tend to think is best for smaller projects. Alternatively you can do something like the angular-sprout project (https://github.com/thedigitalself/angular-sprout) which separates folders by feature rather than according to angular component type, if that makes sense.

0

I have found that the yeoman generated directory structure works well if you separate your component responsibilities. i.e.

app
  |-bower_components
  |-images
  |-scripts
    |---controllers
      |----feature
        |------ *.js
      |----nextFeature
        |------ *.js
    |---services
      |----feature
        |------ *.js
      |----nextFeature
        |------ *js
 |-styles
   |--- *.css
 |-views
   |--- *.html

Then you can have a test directory with almost the same structure.

test
  |-spec
    |---controllers
      |----feature
        |------- *Spec.js
      |----nextFeature
        |------ *Spec.js

etc...

I know this was 9 months ago, but I hope this helps.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.