I've looked at a number of angularJS tutorials and style guides and have found comments like this (from Todd Motto)
Bad:
var app = angular.module('app', []);
app.controller();
app.factory();
Good:
angular
.module('app', [])
.controller()
.factory();
I first learned the "Bad" technique by example and have since seen a couple of reference (other than this one) that say the "Bad" technique is ...well Bad.
Nobody so far in my searches says WHY it is bad?
Edit: Why is this question different? While the differences are subtle between this and the proposed duplicate question, there are two important differences:
'What is the best practice?' is not the same as 'Why is it bad?'...while the accepted answer to the other question elaborates on 'Why', the two questions having the same answer is not sufficient be branded a duplicate.
A vigorous search, using the exact text that I placed as the title to this question did not reveal the proposed duplicate. Perhaps SE should consider allowing "optional titles" to be added to a question to enhance searchablity...but that feature is not in place and someone else asking the same question as mine will still not find the other question.
Global variables in general tend to be considered bad practice, although angular
itself is a global variable so I think that it's honestly not that big of a deal as long as you are consistent.
Problem can arise if you do something like this accidentally:
app = angular.module("app");
// some other file
app = somethingNotAnAngularModule();
External libraries might overwrite the variable app
, etc. etc.
Instead of using the name app
, you could also use a name that is specific to your app...
dustrModule = angular.module("dustr", []);
Chaining is one thing, but if you are splitting up components into separate files you can always get the module with .module
// app.js
angular.module("app", []);
// LoginCtrl.js
angular.module("app").controller("LoginCtrl", LoginCtrl);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With