I've seen this post AngularJS best practices for module declaration? But I am still a little confused about the best way to declare and separate angularJS files for modules, controllers, services etc.
I understand it is good practice to wrap controllers and services in IIFEs because it prevents them from being used out of scope. And it is good practice to separate your controllers and modules into separate files for organization.
My question is, if I have a controller file
myCtrl.js
(function(){
"use strict";
app.controller("myCtrl", ['$scope', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
}]);
})();
A directive file
myDirective.js
(function(){
"use strict";
app.directive("w3TestDirective", function() {
return {
template : "I was made in a directive constructor!"
};
});
})();
What would be the best practice for declaring the module in its own file
myModule.js
Should it be left at global scope so anything can access it as follows:
var app = angular.module("myApp", []);
Or is there a way to wrap this in something like an IIFE to keep it from global scope and yet still be accessible to the other files?