What is the difference between an ng-controller directive and a controller in the route?
angularjs, angularjs-directive, angularjs-routing, javascript
Solution
Controller using a ng-controller directive:
- A new $scope is created on `ng-controller` element.
- Explicit view-to-controller connection
- Visible with inspect element, etc
Controller in a route:
- A new $scope is created per route on the `ng-view` element.
- The controller can request dependencies defined in the route resolve.
- Optional view-to-controller connection. Recommended to have a naming convention that maps routes to controllers to views.
Problem
I worked through the tutorial on the AngularJS website and I noticed that in at step 7, they change how a controller is introduced into the application. Initially, they use a directive: ``` <body ng-controller="PhoneListCtrl"> ... </body> ``` However, it later gets changed to use a `controller` attribute as part of an `ng-route`. ``` $routeProvider. when('/phones', { templateUrl: 'partials/phone-list.html', controller: 'PhoneListCtrl' }). /* rest of routes here */ ``` Here's the git diff where the change is made. Is there a difference between these two techniques?