While this thread sums up the following three code styles:
1)
angular.module('mainCtrl', []);
function MainCrl($scope, $rootScope) {}
2)
angular.module('mainCtrl',[])
.controller('MainCtrl', function($scope, $rootScope)) { ... });
3)
angular.module('mainCtrl',[])
.controller('MainCtrl', ['$scope', '$rootScope', function(scope, rootScope)) { ... }]);
there's a fourth way i've seen in this video that is very appealing for me
4)
var controllers = {}
controllers.mainCtrl = function($scope, $rootScope){ };
app.controller(controllers)
I am leaning towards continuing with 4), will it break if minified or are there any other drawbacks? Should i just go with 3) since it seems to be the standard way of doing it?
Service nameslink Do suffix a service class name with Service . For example, something that gets data or heroes should be called a DataService or a HeroService .
Every Angular app has a root module, conventionally named AppModule, which provides the bootstrap mechanism that launches the application. An app typically contains many functional modules. Modules are decorated with @NgModule.
Naming Convention: *[name of directive] — Upper camel case is used for the directive class, while lower camel case is used for the directive's name. What sets them apart from other directives in Angular 2: Reshape DOM structure by adding or removing existing DOM elements and do not have templates.
My recommendation: Go with option 3, for three reasons:
Option 4 pollutes global namespace, but it is minification-safe if you do it properly1.
Option 3 lets you rename your injectables2, does respect modules, does not pollute global namespace, and does not require any extra work when minifying.
So my winner is option #3.
1 Option 4 - minification-friendly version:
var controllers = {};
controllers.mainCtrl = ['$scope', '$rootScope', function($scope, $rootScope){ ... }];
app.controller(controllers);
2 Renaming injectables:
app.controller('MyCtrl', ['$scope', 'UserService', function($scope, User){ ... }]);
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