EVOLUTION-MANAGER
Edit File: controller.html
<a href='https://github.com/angular/angular.js/edit/v1.3.x/docs/content/guide/controller.ngdoc?message=docs(guide%2FControllers)%3A%20describe%20your%20change...' class='improve-docs btn btn-primary'><i class="glyphicon glyphicon-edit"> </i>Improve this Doc</a> <h1 id="understanding-controllers">Understanding Controllers</h1> <p>In Angular, a Controller is a JavaScript <strong>constructor function</strong> that is used to augment the <a href="guide/scope">Angular Scope</a>.</p> <p>When a Controller is attached to the DOM via the <a href="api/ng/directive/ngController">ng-controller</a> directive, Angular will instantiate a new Controller object, using the specified Controller's <strong>constructor function</strong>. A new <strong>child scope</strong> will be available as an injectable parameter to the Controller's constructor function as <code>$scope</code>.</p> <p>Use controllers to:</p> <ul> <li>Set up the initial state of the <code>$scope</code> object.</li> <li>Add behavior to the <code>$scope</code> object.</li> </ul> <p>Do not use controllers to:</p> <ul> <li>Manipulate DOM — Controllers should contain only business logic. Putting any presentation logic into Controllers significantly affects its testability. Angular has <a href="guide/databinding">databinding</a> for most cases and <a href="guide/directive">directives</a> to encapsulate manual DOM manipulation.</li> <li>Format input — Use <a href="guide/forms">angular form controls</a> instead.</li> <li>Filter output — Use <a href="guide/filter">angular filters</a> instead.</li> <li>Share code or state across controllers — Use <a href="guide/services">angular services</a> instead.</li> <li>Manage the life-cycle of other components (for example, to create service instances).</li> </ul> <h1 id="setting-up-the-initial-state-of-a-scope-object">Setting up the initial state of a <code>$scope</code> object</h1> <p>Typically, when you create an application you need to set up the initial state for the Angular <code>$scope</code>. You set up the initial state of a scope by attaching properties to the <code>$scope</code> object. The properties contain the <strong>view model</strong> (the model that will be presented by the view). All the <code>$scope</code> properties will be available to the template at the point in the DOM where the Controller is registered.</p> <p>The following example demonstrates creating a <code>GreetingController</code>, which attaches a <code>greeting</code> property containing the string <code>'Hola!'</code> to the <code>$scope</code>:</p> <pre><code class="lang-js">var myApp = angular.module('myApp',[]); myApp.controller('GreetingController', ['$scope', function($scope) { $scope.greeting = 'Hola!'; }]); </code></pre> <p>We create an <a href="guide/module">Angular Module</a>, <code>myApp</code>, for our application. Then we add the controller's constructor function to the module using the <code>.controller()</code> method. This keeps the controller's constructor function out of the global scope.</p> <div class="alert alert-info"> We have used an <strong>inline injection annotation</strong> to explicitly specify the dependency of the Controller on the <code>$scope</code> service provided by Angular. See the guide on <a href="guide/di">Dependency Injection</a> for more information. </div> <p>We attach our controller to the DOM using the <code>ng-controller</code> directive. The <code>greeting</code> property can now be data-bound to the template:</p> <pre><code class="lang-js"><div ng-controller="GreetingController"> {{ greeting }} </div> </code></pre> <h1 id="adding-behavior-to-a-scope-object">Adding Behavior to a Scope Object</h1> <p>In order to react to events or execute computation in the view we must provide behavior to the scope. We add behavior to the scope by attaching methods to the <code>$scope</code> object. These methods are then available to be called from the template/view.</p> <p>The following example uses a Controller to add a method to the scope, which doubles a number:</p> <pre><code class="lang-js">var myApp = angular.module('myApp',[]); myApp.controller('DoubleController', ['$scope', function($scope) { $scope.double = function(value) { return value * 2; }; }]); </code></pre> <p>Once the Controller has been attached to the DOM, the <code>double</code> method can be invoked in an Angular expression in the template:</p> <pre><code class="lang-js"><div ng-controller="DoubleController"> Two times <input ng-model="num"> equals {{ double(num) }} </div> </code></pre> <p>As discussed in the <a href="guide/concepts">Concepts</a> section of this guide, any objects (or primitives) assigned to the scope become model properties. Any methods assigned to the scope are available in the template/view, and can be invoked via angular expressions and <code>ng</code> event handler directives (e.g. <a href="api/ng/directive/ngClick">ngClick</a>).</p> <h1 id="using-controllers-correctly">Using Controllers Correctly</h1> <p>In general, a Controller shouldn't try to do too much. It should contain only the business logic needed for a single view.</p> <p>The most common way to keep Controllers slim is by encapsulating work that doesn't belong to controllers into services and then using these services in Controllers via dependency injection. This is discussed in the <a href="guide/di">Dependency Injection</a> <a href="guide/services">Services</a> sections of this guide.</p> <h1 id="associating-controllers-with-angular-scope-objects">Associating Controllers with Angular Scope Objects</h1> <p>You can associate Controllers with scope objects implicitly via the <a href="api/ng/directive/ngController">ngController directive</a> or <a href="api/ngRoute/service/$route">$route service</a>.</p> <h2 id="simple-spicy-controller-example">Simple Spicy Controller Example</h2> <p>To illustrate further how Controller components work in Angular, let's create a little app with the following components:</p> <ul> <li>A <a href="guide/templates">template</a> with two buttons and a simple message</li> <li>A model consisting of a string named <code>spice</code></li> <li>A Controller with two functions that set the value of <code>spice</code></li> </ul> <p>The message in our template contains a binding to the <code>spice</code> model, which by default is set to the string "very". Depending on which button is clicked, the <code>spice</code> model is set to <code>chili</code> or <code>jalapeño</code>, and the message is automatically updated by data-binding.</p> <p> <div> <a ng-click="openPlunkr('examples/example-example6')" class="btn pull-right"> <i class="glyphicon glyphicon-edit"> </i> Edit in Plunker</a> <div class="runnable-example" path="examples/example-example6" module="spicyApp1"> <div class="runnable-example-file" name="index.html" language="html" type="html"> <pre><code><div ng-controller="SpicyController"> <button ng-click="chiliSpicy()">Chili</button> <button ng-click="jalapenoSpicy()">Jalapeño</button> <p>The food is {{spice}} spicy!</p> </div></code></pre> </div> <div class="runnable-example-file" name="app.js" language="js" type="js"> <pre><code>var myApp = angular.module('spicyApp1', []); myApp.controller('SpicyController', ['$scope', function($scope) { $scope.spice = 'very'; $scope.chiliSpicy = function() { $scope.spice = 'chili'; }; $scope.jalapenoSpicy = function() { $scope.spice = 'jalapeño'; }; }]);</code></pre> </div> <iframe class="runnable-example-frame" src="examples/example-example6/index.html" name="example-example6"></iframe> </div> </div> </p> <p>Things to notice in the example above:</p> <ul> <li>The <code>ng-controller</code> directive is used to (implicitly) create a scope for our template, and the scope is augmented (managed) by the <code>SpicyController</code> Controller.</li> <li><code>SpicyController</code> is just a plain JavaScript function. As an (optional) naming convention the name starts with capital letter and ends with "Controller".</li> <li>Assigning a property to <code>$scope</code> creates or updates the model.</li> <li>Controller methods can be created through direct assignment to scope (see the <code>chiliSpicy</code> method)</li> <li>The Controller methods and properties are available in the template (for the <code><div></code> element and its children).</li> </ul> <h2 id="spicy-arguments-example">Spicy Arguments Example</h2> <p>Controller methods can also take arguments, as demonstrated in the following variation of the previous example.</p> <p> <div> <a ng-click="openPlunkr('examples/example-example7')" class="btn pull-right"> <i class="glyphicon glyphicon-edit"> </i> Edit in Plunker</a> <div class="runnable-example" path="examples/example-example7" module="spicyApp2"> <div class="runnable-example-file" name="index.html" language="html" type="html"> <pre><code><div ng-controller="SpicyController"> <input ng-model="customSpice"> <button ng-click="spicy('chili')">Chili</button> <button ng-click="spicy(customSpice)">Custom spice</button> <p>The food is {{spice}} spicy!</p> </div></code></pre> </div> <div class="runnable-example-file" name="app.js" language="js" type="js"> <pre><code>var myApp = angular.module('spicyApp2', []); myApp.controller('SpicyController', ['$scope', function($scope) { $scope.customSpice = "wasabi"; $scope.spice = 'very'; $scope.spicy = function(spice) { $scope.spice = spice; }; }]);</code></pre> </div> <iframe class="runnable-example-frame" src="examples/example-example7/index.html" name="example-example7"></iframe> </div> </div> </p> <p>Notice that the <code>SpicyController</code> Controller now defines just one method called <code>spicy</code>, which takes one argument called <code>spice</code>. The template then refers to this Controller method and passes in a string constant <code>'chili'</code> in the binding for the first button and a model property <code>customSpice</code> (bound to an input box) in the second button.</p> <h2 id="scope-inheritance-example">Scope Inheritance Example</h2> <p>It is common to attach Controllers at different levels of the DOM hierarchy. Since the <a href="api/ng/directive/ngController">ng-controller</a> directive creates a new child scope, we get a hierarchy of scopes that inherit from each other. The <code>$scope</code> that each Controller receives will have access to properties and methods defined by Controllers higher up the hierarchy. See <a href="https://github.com/angular/angular.js/wiki/Understanding-Scopes">Understanding Scopes</a> for more information about scope inheritance.</p> <p> <div> <a ng-click="openPlunkr('examples/example-example8')" class="btn pull-right"> <i class="glyphicon glyphicon-edit"> </i> Edit in Plunker</a> <div class="runnable-example" path="examples/example-example8" module="scopeInheritance"> <div class="runnable-example-file" name="index.html" language="html" type="html"> <pre><code><div class="spicy"> <div ng-controller="MainController"> <p>Good {{timeOfDay}}, {{name}}!</p> <div ng-controller="ChildController"> <p>Good {{timeOfDay}}, {{name}}!</p> <div ng-controller="GrandChildController"> <p>Good {{timeOfDay}}, {{name}}!</p> </div> </div> </div> </div></code></pre> </div> <div class="runnable-example-file" name="app.css" language="css" type="css"> <pre><code>div.spicy div { padding: 10px; border: solid 2px blue; }</code></pre> </div> <div class="runnable-example-file" name="app.js" language="js" type="js"> <pre><code>var myApp = angular.module('scopeInheritance', []); myApp.controller('MainController', ['$scope', function($scope) { $scope.timeOfDay = 'morning'; $scope.name = 'Nikki'; }]); myApp.controller('ChildController', ['$scope', function($scope) { $scope.name = 'Mattie'; }]); myApp.controller('GrandChildController', ['$scope', function($scope) { $scope.timeOfDay = 'evening'; $scope.name = 'Gingerbread Baby'; }]);</code></pre> </div> <iframe class="runnable-example-frame" src="examples/example-example8/index.html" name="example-example8"></iframe> </div> </div> </p> <p>Notice how we nested three <code>ng-controller</code> directives in our template. This will result in four scopes being created for our view:</p> <ul> <li>The root scope</li> <li>The <code>MainController</code> scope, which contains <code>timeOfDay</code> and <code>name</code> properties</li> <li>The <code>ChildController</code> scope, which inherits the <code>timeOfDay</code> property but overrides (hides) the <code>name</code> property from the previous</li> <li>The <code>GrandChildController</code> scope, which overrides (hides) both the <code>timeOfDay</code> property defined in <code>MainController</code> and the <code>name</code> property defined in <code>ChildController</code></li> </ul> <p>Inheritance works with methods in the same way as it does with properties. So in our previous examples, all of the properties could be replaced with methods that return string values.</p> <h2 id="testing-controllers">Testing Controllers</h2> <p>Although there are many ways to test a Controller, one of the best conventions, shown below, involves injecting the <a href="api/ng/service/$rootScope">$rootScope</a> and <a href="api/ng/service/$controller">$controller</a>:</p> <p><strong>Controller Definition:</strong></p> <pre><code class="lang-js">var myApp = angular.module('myApp',[]); myApp.controller('MyController', function($scope) { $scope.spices = [{"name":"pasilla", "spiciness":"mild"}, {"name":"jalapeno", "spiciness":"hot hot hot!"}, {"name":"habanero", "spiciness":"LAVA HOT!!"}]; $scope.spice = "habanero"; }); </code></pre> <p><strong>Controller Test:</strong></p> <pre><code class="lang-js">describe('myController function', function() { describe('myController', function() { var $scope; beforeEach(module('myApp')); beforeEach(inject(function($rootScope, $controller) { $scope = $rootScope.$new(); $controller('MyController', {$scope: $scope}); })); it('should create "spices" model with 3 spices', function() { expect($scope.spices.length).toBe(3); }); it('should set the default value of spice', function() { expect($scope.spice).toBe('habanero'); }); }); }); </code></pre> <p>If you need to test a nested Controller you need to create the same scope hierarchy in your test that exists in the DOM:</p> <pre><code class="lang-js">describe('state', function() { var mainScope, childScope, grandChildScope; beforeEach(module('myApp')); beforeEach(inject(function($rootScope, $controller) { mainScope = $rootScope.$new(); $controller('MainController', {$scope: mainScope}); childScope = mainScope.$new(); $controller('ChildController', {$scope: childScope}); grandChildScope = childScope.$new(); $controller('GrandChildController', {$scope: grandChildScope}); })); it('should have over and selected', function() { expect(mainScope.timeOfDay).toBe('morning'); expect(mainScope.name).toBe('Nikki'); expect(childScope.timeOfDay).toBe('morning'); expect(childScope.name).toBe('Mattie'); expect(grandChildScope.timeOfDay).toBe('evening'); expect(grandChildScope.name).toBe('Gingerbread Baby'); }); }); </code></pre>