The ngController
directive attaches a controller class to the view. This is a key aspect of how angular
supports the principles behind the Model-View-Controller design pattern.
MVC components in angular:
ngController
directive specifies a Controller class; the class contains business
logic behind the application to decorate the scope with functions and valuesNote that you can also attach controllers to the DOM by declaring it in a route definition
via the $route service. A common mistake is to declare the controller
again using ng-controller
in the template itself. This will cause the controller to be attached
and executed twice.
<ANY
ng-controller="expression">
...
</ANY>
Param | Type | Details |
---|---|---|
ngController | expression |
Name of a constructor function registered with the current $controllerProvider or an expression that on the current scope evaluates to a constructor function. The controller instance can be published into a scope property by specifying
If the current |
Here is a simple form for editing user contact information. Adding, removing, clearing, and greeting are methods declared on the controller (see source tab). These methods can easily be called from the angular markup. Any changes to the data are automatically reflected in the View without the need for a manual update.
Two different declaration styles are included below:
this
:
ng-controller="SettingsController1 as settings"
$scope
into the controller:
ng-controller="SettingsController2"
The second option is more common in the Angular community, and is generally used in boilerplates and in this guide. However, there are advantages to binding properties directly to the controller and avoiding scope.
controller as
makes it obvious which controller you are accessing in the template when
multiple controllers apply to an element..
in the bindings, you don't have to worry about prototypal
inheritance masking primitives.This example demonstrates the controller as
syntax.
<div id="ctrl-as-exmpl" ng-controller="SettingsController1 as settings">
<label>Name: <input type="text" ng-model="settings.name"/></label>
<button ng-click="settings.greet()">greet</button><br/>
Contact:
<ul>
<li ng-repeat="contact in settings.contacts">
<select ng-model="contact.type" aria-label="Contact method" id="select_{{$index}}">
<option>phone</option>
<option>email</option>
</select>
<input type="text" ng-model="contact.value" aria-labelledby="select_{{$index}}" />
<button ng-click="settings.clearContact(contact)">clear</button>
<button ng-click="settings.removeContact(contact)" aria-label="Remove">X</button>
</li>
<li><button ng-click="settings.addContact()">add</button></li>
</ul>
</div>
This example demonstrates the "attach to $scope
" style of controller.
<div id="ctrl-exmpl" ng-controller="SettingsController2">
<label>Name: <input type="text" ng-model="name"/></label>
<button ng-click="greet()">greet</button><br/>
Contact:
<ul>
<li ng-repeat="contact in contacts">
<select ng-model="contact.type" id="select_{{$index}}">
<option>phone</option>
<option>email</option>
</select>
<input type="text" ng-model="contact.value" aria-labelledby="select_{{$index}}" />
<button ng-click="clearContact(contact)">clear</button>
<button ng-click="removeContact(contact)">X</button>
</li>
<li>[ <button ng-click="addContact()">add</button> ]</li>
</ul>
</div>