Reposting from https://medium.com/@kevinle/how-i-write-my-angular-modules-5cce0dfb2254

I used to write my Angular modules as follow:

//In app.js which is my starting (entry) point
var app = angular.module('myApp', [...])
.config(function($routeProvider, $locationProvider) {
//some code
})
.run(function($rootScope, $route, $location, editableOptions) {
//some code
});

Then in my controller controller.js:

//controller.js
app.controller('myController', ['$scope', function($scope) {
$scope.foo = function() {
//some code
};
}]);

And so on for directives and services. Probably not so bad. You probably pick this up from reading post blog like this and some answers on this StackOverflow question advocates this style.

The only problem is that the variable app now has visibility throughout all my files. To eliminate the usage of this variable app, I do like so:

//app.js
angular.module('myApp', [...])
.config(function($routeProvider, $locationProvider) {
//some code
})
.run(function($rootScope, $route, $location, editableOptions) {
//some code
});

Then in controller.js:

//controller.js
angular.module('myApp').controller('myController', ['$scope', function($scope) {
$scope.foo = function() {
//some code
};
}]);

Again, and so on for services and directives. Again probably nothing wrong. But I finally settled on using IIFEs, not just IIFes but I want to eliminate the global variable angular from appearing anywhere in my functions. Let’s look at this:

//app.js
(function(module) {
'use strict';
module.config(['$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider) {
// some code
}
]).run(['$rootScope', '$route', '$location', 'editableOptions',
function($rootScope, $route, $location, editableOptions) {
// some code
}
]);
})(angular.module('myApp', [...]));

Then in controller.js:

//controller.js
(function(module) {
'use strict';
module.controller('myController', ['$scope',
function($scope) {
//some code
}
]);
})(angular.module('myApp'));

What’s the improvement here, you ask? No more global variables, even only the well known one angular is no where to be found inside any of the functions. This global variable angular only appears as an argument that gets passed in to the IIFEs.

I am going to stick with this style.