11

I'm new to angular, so please bear with me. I was reading an article/documentation the other day that highlighted the best way to structure modules in your app, and can only remember it loosely.

App.controllers
App.services
....

angular.module('App', [App.controllers, App.services ...);

This code sample is very likely to be incorrect, but the point was to group controllers, services etc together in one namespace.

Could anyone expand upon this approach?

alexandroid
  • 1,469
  • 2
  • 15
  • 32
leepowell
  • 3,838
  • 8
  • 27
  • 35
  • Ignore - I found it on the [Angular documentation](http://docs.angularjs.org/guide/module) of all places! – leepowell Jan 31 '13 at 08:46
  • I wondered the same about here is some helpful info [Angular Best Practices](http://stackoverflow.com/q/20802798/1959948) – Dalorzo May 17 '14 at 22:42
  • 1
    Have a look at this project starter pack https://github.com/jofftiquez/angular-app-starter-pack – jofftiquez Oct 28 '15 at 13:56

4 Answers4

38

Enterprise project organization

The way I organize my angular project is:

/app
  /img         # application-level images
  /css         # application-level css styles
  /js          # application-level javascripts
  /modules             # application modules
          /gallery               # independent module with its own infrastructure
                 /controllers    # gallery module's controllers
                 /css            # gallery module's css styles
                 /directives     # gallery module's directives
                 /img            # gallery module's images
                 /filters        # gallery module's filters
                 /services       # gallery module's services
                 /views          # gallery module's views (htmls)
                 / ...           # other gallery module component folders
                 galleryMod.js   # the module itself

          /user                  # independent module with its own infrastructure
                 /controllers    # user module's controllers
                 / ...           # other user module component folders
                 userMod.js      # the module itself

          / ...                  # other modules

  / ...                # other application-level folders
  index.html

Alternative enterprise project organization (simplified)

/app
  /img         # application-level images
  /css         # application-level css styles
  /js          # application-level javascripts
  /modules             # application modules
          /gallery               # independent module with its own infrastructure
                 /js             # gallery module's javascripts (includes 
                                 # services.js, directives.js, filters.js, ...)
                 /css            # gallery module's css styles
                 /img            # gallery module's images
                 /views          # gallery module's views (htmls, "partials")
                 / ...           # other gallery module component folders
                 galleryMod.js   # the module itself

          /user                  # independent module with its own infrastructure
                 /controllers    # user module's controllers
                 / ...           # other user module component folders
                 userMod.js      # the module itself

          / ...                  # other modules

  / ...                # other application-level folders
  index.html

Middle project organization (without modules)

/app
  /img            # application's images
  /css            # application's css styles
  /controllers    # application's controllers
  /directives     # application's directives
  /filters        # application's filters
  /services       # application's services
  /views          # application's views (htmls)
  / ...           # other component folders
  index.html

Simple project organization (just like a seed)

/app
  /img            # application's images
  /css            # application's css styles
  /js             # application's javascripts (includes 
                  # services.js, directives.js, filters.js, ...)
  /views          # application's views (htmls), e.g. partials
  / ...           # other component folders
  index.html

Use the way your project needs to be organized and don't choose the way that will unnecessarily complicate your project.

pleerock
  • 18,322
  • 16
  • 103
  • 128
  • I like the way you differentiate between small and large project. Many discussions evolve around using feature-based structure or not and this is very helpful in comparing different approaches. But would it be terrible if one could drop the main "app" folder? Could somebody explain to me why this is a necessity? – Daniel Apr 24 '15 at 08:36
  • 1
    the app folder is used because in your project can be lot of other files that are not related to your app source code. outside of your app should go things like tests(e2e, unit), build configuration, settings, gulp/grunt/bower/package.json files and lot of other files that can be in your project. You can put these files and folders in a single directory, and not to use app/ folder, but file structure will be not be not as good as with app/ folder with separated source code of your app. You can also use `src/` as alternative to `app` name – pleerock Apr 24 '15 at 09:27
16

This approach is provided by Angular Seed and Its just one of the ways to orginize app structure. It is useful for debugging: If you see bug in some service you go to services.js and catch It.

Brain Ford in his article Building Huuuuuge Apps with AngularJS wrote:

The only remaining question is how to subdivide controllers, directives, services, and filters into modules. The Angular Seed puts filters, services, and directives into separate modules, but that seems a bit silly to me. Depending on the app, I'd be more inclined to organize modules by page/route. From a performance perspective, it doesn't really matter how you organize your modules, so choose whatever method best suits your project.

He also propose different app structure where each directive or service is an one separate file (see article above).

Maxim Ponomarev
  • 686
  • 10
  • 18
12

From John Pappa's AngularJS Style Guide. This is becoming the 'Angular standard structure' for larger apps.

Folders-by-Feature Structure: Create folders named for the feature they represent. When a folder grows to contain more than 7 files, start to consider creating a folder for them. Your threshold may be different, so adjust as needed.

Why?: A developer can locate the code, identify what each file represents at a glance, the structure is flat as can be, and there is no repetitive nor redundant names.

Why?: The LIFT guidelines are all covered.

Why?: Helps reduce the app from becoming cluttered through organizing the content and keeping them aligned with the LIFT guidelines.

Why?: When there are a lot of files (10+) locating them is easier with a consistent folder structures and more difficult in flat structures.

/**
 * recommended
 */

app/
    app.module.js
    app.config.js
    app.routes.js
    components/       
        calendar.directive.js  
        calendar.directive.html  
        user-profile.directive.js  
        user-profile.directive.html  
    layout/
        shell.html      
        shell.controller.js
        topnav.html      
        topnav.controller.js       
    people/
        attendees.html
        attendees.controller.js  
        speakers.html
        speakers.controller.js
        speaker-detail.html
        speaker-detail.controller.js
    services/       
        data.service.js  
        localstorage.service.js
        logger.service.js   
        spinner.service.js
    sessions/
        sessions.html      
        sessions.controller.js
        session-detail.html
        session-detail.controller.js  
Sean McClory
  • 4,195
  • 3
  • 32
  • 35
1

This folder structure allows me to scale Angular and find every file quickly by understanding and grouping their overall functionality.

 /app
    /features    
           /product   #one folder per entity
               /get-products component
               /create-product component
               /update-product component
               /delete-product component
               /state #ngrx state
                  /actions
                  /effects
                  /reducers
                  /selector
                  index
    /models 
           auth
           user
           product
           # additional.model.ts 

    /services
           backend service
           api service
           auth service
           firebase service
           auth-guard service
           # additional.service.ts
    /store
          /actions
          /effects
          /reducers
          app-store
          index
    /utilities
          router.animation
          customAngularMaterial module
          uuid
          validator
          pipe
          directive
          # additional utilities
    /views
          /admin component
          /auth  component
          /header component
          /footer component
          /navbar  component
          /profiles  component
          /home  component
          /contactus  component
          #additional views
    app-routing.module
    app.component
    app.module
  /assets
      /css      #custom styles.css
      /icons    #svg files
      /favicon.ico
      /images
      #additional assets
main
index     #material icons
styles    #angular material prebuilt theme
Domiserver
  • 441
  • 6
  • 11