EVOLUTION-MANAGER
Edit File: ngController.html
<a href='https://github.com/angular/angular.js/edit/v1.3.x/src/ng/directive/ngController.js?message=docs(ngController)%3A%20describe%20your%20change...#L3' class='improve-docs btn btn-primary'><i class="glyphicon glyphicon-edit"> </i>Improve this Doc</a> <a href='https://github.com/angular/angular.js/tree/v1.3.9/src/ng/directive/ngController.js#L3' class='view-source pull-right btn btn-primary'> <i class="glyphicon glyphicon-zoom-in"> </i>View Source </a> <header class="api-profile-header"> <h1 class="api-profile-header-heading">ngController</h1> <ol class="api-profile-header-structure naked-list step-list"> <li> - directive in module <a href="api/ng">ng</a> </li> </ol> </header> <div class="api-profile-description"> <p>The <code>ngController</code> 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.</p> <p>MVC components in angular:</p> <ul> <li>Model — Models are the properties of a scope; scopes are attached to the DOM where scope properties are accessed through bindings.</li> <li>View — The template (HTML with data bindings) that is rendered into the View.</li> <li>Controller — The <code>ngController</code> directive specifies a Controller class; the class contains business logic behind the application to decorate the scope with functions and values</li> </ul> <p>Note that you can also attach controllers to the DOM by declaring it in a route definition via the <a href="api/ngRoute/service/$route">$route</a> service. A common mistake is to declare the controller again using <code>ng-controller</code> in the template itself. This will cause the controller to be attached and executed twice.</p> </div> <div> <h2>Directive Info</h2> <ul> <li>This directive creates new scope.</li> <li>This directive executes at priority level 500.</li> </ul> <h2 id="usage">Usage</h2> <div class="usage"> <ul> <li>as attribute: <pre><code><ANY ng-controller=""> ... </ANY></code></pre> </li> </div> <section class="api-section"> <h3>Arguments</h3> <table class="variables-matrix input-arguments"> <thead> <tr> <th>Param</th> <th>Type</th> <th>Details</th> </tr> </thead> <tbody> <tr> <td> ngController </td> <td> <a href="" class="label type-hint type-hint-expression">expression</a> </td> <td> <p>Name of a constructor function registered with the current <a href="api/ng/provider/$controllerProvider">$controllerProvider</a> or an <a href="guide/expression">expression</a> that on the current scope evaluates to a constructor function.</p> <p>The controller instance can be published into a scope property by specifying <code>ng-controller="as propertyName"</code>.</p> <p>If the current <code>$controllerProvider</code> is configured to use globals (via <a href="api/ng/provider/$controllerProvider#allowGlobals"><code>$controllerProvider.allowGlobals()</code></a>), this may also be the name of a globally accessible constructor function (not recommended).</p> </td> </tr> </tbody> </table> </section> <h2 id="example">Example</h2><p>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.</p> <p>Two different declaration styles are included below:</p> <ul> <li>one binds methods and properties directly onto the controller using <code>this</code>: <code>ng-controller="SettingsController1 as settings"</code></li> <li>one injects <code>$scope</code> into the controller: <code>ng-controller="SettingsController2"</code></li> </ul> <p>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.</p> <ul> <li>Using <code>controller as</code> makes it obvious which controller you are accessing in the template when multiple controllers apply to an element.</li> <li>If you are writing your controllers as classes you have easier access to the properties and methods, which will appear on the scope, from inside the controller code.</li> <li>Since there is always a <code>.</code> in the bindings, you don't have to worry about prototypal inheritance masking primitives.</li> </ul> <p>This example demonstrates the <code>controller as</code> syntax.</p> <p> <div> <a ng-click="openPlunkr('examples/example-ngControllerAs')" class="btn pull-right"> <i class="glyphicon glyphicon-edit"> </i> Edit in Plunker</a> <div class="runnable-example" path="examples/example-ngControllerAs" name="ngControllerAs" module="controllerAsExample"> <div class="runnable-example-file" name="index.html" language="html" type="html"> <pre><code><div id="ctrl-as-exmpl" ng-controller="SettingsController1 as settings"> Name: <input type="text" ng-model="settings.name"/> [ <a href="" ng-click="settings.greet()">greet</a> ]<br/> Contact: <ul> <li ng-repeat="contact in settings.contacts"> <select ng-model="contact.type"> <option>phone</option> <option>email</option> </select> <input type="text" ng-model="contact.value"/> [ <a href="" ng-click="settings.clearContact(contact)">clear</a> | <a href="" ng-click="settings.removeContact(contact)">X</a> ] </li> <li>[ <a href="" ng-click="settings.addContact()">add</a> ]</li> </ul> </div></code></pre> </div> <div class="runnable-example-file" name="app.js" language="js" type="js"> <pre><code>angular.module('controllerAsExample', []) .controller('SettingsController1', SettingsController1); function SettingsController1() { this.name = "John Smith"; this.contacts = [ {type: 'phone', value: '408 555 1212'}, {type: 'email', value: 'john.smith@example.org'} ]; } SettingsController1.prototype.greet = function() { alert(this.name); }; SettingsController1.prototype.addContact = function() { this.contacts.push({type: 'email', value: 'yourname@example.org'}); }; SettingsController1.prototype.removeContact = function(contactToRemove) { var index = this.contacts.indexOf(contactToRemove); this.contacts.splice(index, 1); }; SettingsController1.prototype.clearContact = function(contact) { contact.type = 'phone'; contact.value = ''; };</code></pre> </div> <div class="runnable-example-file" name="protractor.js" type="protractor" language="js"> <pre><code>it('should check controller as', function() { var container = element(by.id('ctrl-as-exmpl')); expect(container.element(by.model('settings.name')) .getAttribute('value')).toBe('John Smith'); var firstRepeat = container.element(by.repeater('contact in settings.contacts').row(0)); var secondRepeat = container.element(by.repeater('contact in settings.contacts').row(1)); expect(firstRepeat.element(by.model('contact.value')).getAttribute('value')) .toBe('408 555 1212'); expect(secondRepeat.element(by.model('contact.value')).getAttribute('value')) .toBe('john.smith@example.org'); firstRepeat.element(by.linkText('clear')).click(); expect(firstRepeat.element(by.model('contact.value')).getAttribute('value')) .toBe(''); container.element(by.linkText('add')).click(); expect(container.element(by.repeater('contact in settings.contacts').row(2)) .element(by.model('contact.value')) .getAttribute('value')) .toBe('yourname@example.org'); });</code></pre> </div> <iframe class="runnable-example-frame" src="examples/example-ngControllerAs/index.html" name="example-ngControllerAs"></iframe> </div> </div> </p> <p>This example demonstrates the "attach to <code>$scope</code>" style of controller.</p> <p> <div> <a ng-click="openPlunkr('examples/example-ngController')" class="btn pull-right"> <i class="glyphicon glyphicon-edit"> </i> Edit in Plunker</a> <div class="runnable-example" path="examples/example-ngController" name="ngController" module="controllerExample"> <div class="runnable-example-file" name="index.html" language="html" type="html"> <pre><code><div id="ctrl-exmpl" ng-controller="SettingsController2"> Name: <input type="text" ng-model="name"/> [ <a href="" ng-click="greet()">greet</a> ]<br/> Contact: <ul> <li ng-repeat="contact in contacts"> <select ng-model="contact.type"> <option>phone</option> <option>email</option> </select> <input type="text" ng-model="contact.value"/> [ <a href="" ng-click="clearContact(contact)">clear</a> | <a href="" ng-click="removeContact(contact)">X</a> ] </li> <li>[ <a href="" ng-click="addContact()">add</a> ]</li> </ul> </div></code></pre> </div> <div class="runnable-example-file" name="app.js" language="js" type="js"> <pre><code>angular.module('controllerExample', []) .controller('SettingsController2', ['$scope', SettingsController2]); function SettingsController2($scope) { $scope.name = "John Smith"; $scope.contacts = [ {type:'phone', value:'408 555 1212'}, {type:'email', value:'john.smith@example.org'} ]; $scope.greet = function() { alert($scope.name); }; $scope.addContact = function() { $scope.contacts.push({type:'email', value:'yourname@example.org'}); }; $scope.removeContact = function(contactToRemove) { var index = $scope.contacts.indexOf(contactToRemove); $scope.contacts.splice(index, 1); }; $scope.clearContact = function(contact) { contact.type = 'phone'; contact.value = ''; }; }</code></pre> </div> <div class="runnable-example-file" name="protractor.js" type="protractor" language="js"> <pre><code>it('should check controller', function() { var container = element(by.id('ctrl-exmpl')); expect(container.element(by.model('name')) .getAttribute('value')).toBe('John Smith'); var firstRepeat = container.element(by.repeater('contact in contacts').row(0)); var secondRepeat = container.element(by.repeater('contact in contacts').row(1)); expect(firstRepeat.element(by.model('contact.value')).getAttribute('value')) .toBe('408 555 1212'); expect(secondRepeat.element(by.model('contact.value')).getAttribute('value')) .toBe('john.smith@example.org'); firstRepeat.element(by.linkText('clear')).click(); expect(firstRepeat.element(by.model('contact.value')).getAttribute('value')) .toBe(''); container.element(by.linkText('add')).click(); expect(container.element(by.repeater('contact in contacts').row(2)) .element(by.model('contact.value')) .getAttribute('value')) .toBe('yourname@example.org'); });</code></pre> </div> <iframe class="runnable-example-frame" src="examples/example-ngController/index.html" name="example-ngController"></iframe> </div> </div> </p> </div>