nmod68
nmod68

Reputation: 3

Angular routing definitions in partials instead of app.js

I recently started a new project using Angular Seed Project (https://github.com/angular/angular-seed/) and I noticed something strange. I'm quite new to Angular but in all the tutorials I did the routing part was always in the "main" JS file (usually named app.js). However here, in this "seed project" the only route definition in app.js is :

config(['$routeProvider', function($routeProvider) {  $routeProvider.otherwise({redirectTo: '/view1'});}]);

The other routing definitions are in each partials Js file (within the controller) by example file view1/view1.js :

angular.module('myApp.view1', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/view1', {
    templateUrl: 'view1/view1.html',
    controller: 'View1Ctrl'
  });
}])

and view2/view2.js :

angular.module('myApp.view2', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/view2', {
    templateUrl: 'view2/view2.html',
    controller: 'View2Ctrl'
  });
}])

So I understand that since all the Js files are properly included the result is the same as if all the routing was done in app.js but I wonder why it is structured like this. The angular seed is meant to be a "best practices" starter for Angular. So here is my question, is it considered a good practice to split the routing definitions in every partial JS file instead of defining everything in app.js ? Why ?

Thank you guys ! :)

Upvotes: 0

Views: 176

Answers (1)

JoMendez
JoMendez

Reputation: 884

You can use both approaches, both are correct, and I don't think there is more advantages in use one or the other more than the organization of the code, It won't even affect the performance of your app. For small demo applications (tutorial) is easier to put all the routes together, but for a real life big or scalable applications the approach used in the angular seed seem to be more centralized, where you keep the controller, the router and the view in a folder (module)

Upvotes: 1

Related Questions