Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between an ng-controller directive and a controller in the route?

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?

like image 285
austin Avatar asked Jan 29 '14 14:01

austin


People also ask

What is the difference between AngularJS directives and controllers?

A controller is usually used to contain and maintain the logic for your view, which gets bound to your view via $scope. A directive is something that you might use repeatedly and is called in your view directly through the directive name which you can pass in as an attribute.

What is the difference between the scopes of a directive and the scopes of a controller?

No difference. It is same object.

What is the difference between controller and link in directives?

Answer:The link option is just a shortcut to setting up a post-link function. controller: The directive controller can be passed to another directive linking/compiling phase. It can be injected into other directices as a mean to use in inter-directive communication.

What are ng directives?

The ng-model directive binds the value of HTML controls (input, select, textarea) to application data. The ng-model directive can also: Provide type validation for application data (number, email, required). Provide status for application data (invalid, dirty, touched, error). Provide CSS classes for HTML elements.


2 Answers

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.
like image 156
Bob Fanger Avatar answered Oct 07 '22 21:10

Bob Fanger


One of well-known feature of Angularjs is Single-Page Applications.

If you assign ng-controller attribute directly on the page:

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

you can't switch controllers easily for other tasks.

So, use route to switch controllers is one of important step in learning Angular Single-Page feature.

You can have same layout and one different element by using route and ng-view directive.

$routeProvider.
    when('/phones', {
        templateUrl: 'partials/phone-list.html',
        controller: 'PhoneListCtrl'
    }).
    when('/tablets', { 
        templateUrl: 'partials/tablet-list.html',
        controller: 'TabletListCtrl'
    }).

If '/phones'

<div ng-view></div>

will include your 'partials/phone-list.html' template and set 'PhoneListCtrl' as div controller

The same:

If '/tablets'

<div ng-view></div>

will include your 'partials/tablet-list.html' template and set 'TabletListCtrl' as div controller

This is the difference between two.

like image 43
Chen-Tsu Lin Avatar answered Oct 07 '22 21:10

Chen-Tsu Lin