Search code examples
angularjsangularjs-directiveroute-provider

Controller in $routeProvider.when vs ng-controller in template


New to Angular.js.

What's the difference of passing the controller as

app.config(function($routeProvider, $locationProvider){ $locationProvider.html5Mode(false); $routeProvider .when('/login', {templateUrl: '/templates/profile/login.html', controller: 'angLoginController'}); });

vs declaring the controller using ng-controller directive in the template itself?

<body ng-controller="angLoginController"> .... </body>


Solution

  • In short: Separation of concerns. It would be easier to maintain if you place all controllers respective to the view in the router. You need to edit all in one place.

    In $routeProvider

    This gives you flexibility to manage all controllers and views respective to that particular route. In this case you can manage controllers in one place.

    In second case: inside view

    if you place it inside individual view, Then you need to go to that view and edit them.