Why is setting an app variable a 'bad practice' in angularJS? -


i've looked @ number of angularjs tutorials , style guides , have found comments (from todd motto)

bad: var app = angular.module('app', []); app.controller(); app.factory();  good: angular   .module('app', [])   .controller()   .factory(); 

i first learned "bad" technique example , have since seen couple of reference (other one) "bad" technique ...well bad.

nobody far in searches says why bad?

edit: why question different? while differences subtle between , proposed duplicate question, there 2 important differences:

  1. 'what best practice?' not same 'why bad?'...while accepted answer other question elaborates on 'why', 2 questions having same answer not sufficient branded duplicate.

  2. a vigorous search, using exact text placed title question did not reveal proposed duplicate. perhaps se should consider allowing "optional titles" added question enhance searchablity...but feature not in place , else asking same question mine still not find other question.

global variables in general tend considered bad practice, although angular global variable think it's not that big of deal long consistent.

problem can arise if accidentally:

app = angular.module("app"); // other file app = somethingnotanangularmodule(); 

external libraries might overwrite variable app, etc. etc.

instead of using name app, use name specific app...

dustrmodule = angular.module("dustr", []); 

chaining 1 thing, if splitting components separate files can get module .module

// app.js angular.module("app", []);  // loginctrl.js angular.module("app").controller("loginctrl", loginctrl); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -