30

On a large AngularJS application having all my controllers in a single "controllers.js" file seems a little un-maintainable to me. Is there a better way to do this such as:

\js\controllers\myController.js
\js\controllers\yourController.js
\js\controllers\ourController.js

and that also applies for filters, services, directives etc...

Tom Robinson
  • 8,348
  • 9
  • 58
  • 102
AndrewMcLagan
  • 13,459
  • 23
  • 91
  • 158

6 Answers6

49

There are lot's of ways to organize your code. You can look in the following links

You can follow their standard or you can make your own.

Try to follow the following guidelines:

  • Contollers shouldn't be too long, if it's too long then it is handling multiple responsibilities
  • Try to use Directives and Services in your system to reuse your code/logic
  • Directives are the most powerful things in Angualrjs, try to get maximum advantage of it.
  • Write Tests; even better you can try to practice TDD with AngularJS
Imrul
  • 3,456
  • 5
  • 32
  • 27
  • organizing things by type is good for small projects, but it doesn't help if you want to borrow some of the features that you wrote in the past. That's where the origination by functionality, ex. module, comes into play. I really enjoy reading the second article in your list. Thanks for sharing. – windmaomao Oct 21 '14 at 21:12
  • Your third link is a 404 in 2018. – ale10ander Mar 02 '18 at 18:27
  • @ale10ander Thanks for pointing that out. The structure was for Angular 1, will update my Answer. – Imrul Mar 12 '18 at 06:55
9

You can manage it like module wise!!

For example , take user view , you make one directory, here its name is user!!

user // directory , now put all controller ,service and directive file into it !! 

-- userController.js    //  controller file 

-- userService.js       // service file

-- userDirective.js     // directive file

-- views                // make directory, and put all html file regarding that module into this

  --users.html          // html file

Hope this will help you!!

Shivali Patel
  • 884
  • 8
  • 12
3

See how these two starter projects organize files for a larger-scale application:

Alexander Puchkov
  • 5,913
  • 4
  • 34
  • 48
3

You may wish to have a look at this community-driven guide.

The guide describes best practices for organizing the directory structure of a large AngularJS application.

It also makes recommendations on naming and structuring AngularJS modules, controllers, directives, filters and services.

It's also worth it to check out a tool like Lineman.js with the AngularJS application template.

For enterprise AngularJS projects, you may wish to look at this kickstarter which is based on ng-boilerplate.

user2845946
  • 1,755
  • 29
  • 38
3

There's a nice document out there from Google's own team that back up Shivali's example: https://docs.google.com/document/d/1XXMvReO8-Awi1EZXAXS4PzDzdNvV6pGcuaF4Q9821Es/pub

Something like this:

sampleapp/
    app.css
    app.js                top-level configuration, route def’ns for the app
    app-controller.js
    app-controller_test.js
    components/
    adminlogin/                                
            adminlogin.css                styles only used by this component
            adminlogin.js              optional file for module definition
            adminlogin-directive.js
            adminlogin-directive_test.js        
                    private-export-filter/
                            private-export-filter.js
                            private-export-filter_test.js
    userlogin/
    somefilter.js
    somefilter_test.js
    userlogin.js
    userlogin.css                
    userlogin.html                
    userlogin-directive.js
    userlogin-directive_test.js
    userlogin-service.js
    userlogin-service_test.js                
            index.html
    subsection1/
    subsection1.js
    subsection1-controller.js
                    subsection1-controller_test.js
                    subsection1_test.js                         
                    subsection1-1/                        
                            subsection1-1.css
                            subsection1-1.html
                            subsection1-1.js
                            subsection1-1-controller.js
    subsection1-1-controller_test.js
                    subsection1-2/                        
            subsection2/
    subsection2.css
    subsection2.html
    subsection2.js
    subsection2-controller.js
    subsection2-controller_test.js
            subsection3/
                    subsection3-1/
    etc...
marksyzm
  • 5,281
  • 2
  • 29
  • 27
0

Check this, build your angular app with CoffeeScript, SCSS.

https://github.com/nhim175/modular-angular-skeleton/

Thịnh Phạm
  • 2,528
  • 5
  • 26
  • 39