Closed davisford closed 10 years ago
controllers is just a part of a module todo.js should be used to declare module dependencies It could also contain config-blocks, run-blocks, perhaps also routes and small services. I sometimes use it to initialize a service in the module, example:
// todo.js
angular.module('app.todo', []);
.run(function (ping) {
$rootScope.ping = ping;
})
.config(function ($routeProvider) {
'use strict';
$routeProvider
.when('/todo', {
controller: 'TodoCtrl',
templateUrl: '/app/todo/todo.html'
})
.otherwise({
redirectTo: '/todo'
});
// todo-service.js
angular.module('app.todo');
.factory('ping', function () {
'use strict';
return { pong: function () { console.log('pong'); } }
});
// todo-ctrl.js
angular.module('app.todo');
.controller('TodoCtrl', function (ping) {
$rootScope.ping.pong();
...
});
Thanks for the response. Splitting out the $routeProvider
configuration into separate module files is indeed a nicer way to do that, as opposed to dumping them all in app.js
. Good tip. I'll close this out.
@davisford having a separate file for the module definition is good for possible future scalability, I also like the consistency of always having an entry point file for each module/folder with the same name as the module/folder - which gives you the benefit of easy navigation in your editor :)
Hi, thanks for the bootstrap; I was wondering what your rationale is for creating the
todo.js
file in the sample app with the todo sub-section? In it, you just create the module ... i.e.But curious why not just put that in with the controller,
todo-controller.js
?I suppose if a sub-section had multiple controllers in it, then it would be necessary because you only want to define the module one time, but I am just now porting a sizable app over to this new structure, and wanted to make sure I understood what the point of that file was -- i.e. why not merge the module creation with the controller ?