0

Fairly simple question, I can't seem to find a definitive answer. At the minute I've my module declared in one file :

var module = angular.module("app", ["agGrid", "ngAnimate", "ngSanitize", "ui.bootstrap"]);

and my controller in another :

angular.module("app").controller("mainCtrl", ["$scope", "$timeout", "dateFilter", "$http", "shareDataService", "getDataService", function ($scope, $timeout, dateFilter, $http, shareDataService, getDataService) {

Is this good structure or a waste of time and space?

0

3 Answers 3

2

Single Responsibility

Define 1 component per file.

The following example defines the app module and its dependencies, defines a controller, and defines a factory all in the same file.

Avoid this

angular
    .module('app', ['ngRoute'])
    .controller('SomeController', SomeController)
    .factory('someFactory', someFactory);

function SomeController() { }

function someFactory() { }

The same components are now separated into their own files.

Do this

// app.module.js
angular
    .module('app', ['ngRoute']);

// some.controller.js
angular
    .module('app')
    .controller('SomeController', SomeController);

function SomeController() { }

// someFactory.js
angular
    .module('app')
    .factory('someFactory', someFactory);

function someFactory() { }

https://github.com/johnpapa/angular-styleguide#style-y001

Sign up to request clarification or add additional context in comments.

Comments

1

It's a good practice to keep them separate. This way, you don't end up mixing the module(s) definition and controllers / services / directives.

here you get some of the best practices in angular js -

Instead of slicing your app across horizontals that can't be broken up, group your code into related bundles. This way if you remove a module, your app still works.

https://github.com/angular/angular.js/wiki/Best-Practices

2 Comments

Thanks, I'll accept this as an answer seeing as it is definitive!
Bundling vs slicing. I always hated slicing.
1

Having separate files for each type of component is ideal. I follow the structure in development:

app.js //where the module resides
routes.js //consists of routes
controllers/
services/ 
factories/
filters/
directives/

Define your module:

var app = angular.module();

Then use 'app' to declare other nested js in separate files, example:

app.directive()

However, in production, it is preferable to use task runner (eg gulp) to combine all the files and minify the final file.

2 Comments

Yeah this is the structure I'm currently following, declaring my module in a separate file and using it for nested files, services, controllers etc etc. I have been reading a basic but good tutorial on best practices though (trochette.github.io/Angular-Design-Patterns-Best-Practices/#/…) and in it it notes a better structure (for growing Angular projects) would be to have a modular structure, so each dependency used for "Login" (it's controller, service etc) are in the same folder.
Yes for very large projects, functionality based modularity, that is, controllers, services etc related to a functionality in a separate folder is ideal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.