AngularJS: l'errore della carenza di moduli

AngularJS: l'errore della carenza di moduli

Un errore che si commette spesso con AngularJS è quello di legare tutta l'implementazione ad un unico modulo.

È stato scritto che:

It's common when starting out to hang everything off of the main module. This works fine when starting a small app, but it quickly becomes unmanageable.

Mark Meyer

In pratica abbiamo:


var app = angular.module('MyApp',[]);
 
app.service('MyService',function() {
	//... 
});

app.controller('MyCtrl', ['$scope', 'MyService', function($scope, MyService) {
	//...
}]);

In realtà dovremmo usare più moduli:


var services = angular.module('Services',[]);

services.service('MyService', function() {
	//...
});

var controllers = angular.module('Controllers',['Services']);

controllers.controller('MyCtrl', ['$scope', 'MyService', function($scope, MyService) {
	//...
}]);

var app = angular.module('MyApp',['Controllers', 'Services']);

Torna su