EVOLUTION-MANAGER
Edit File: step_02.html
<a href='https://github.com/angular/angular.js/edit/v1.3.x/docs/content/tutorial/step_02.ngdoc?message=docs(tutorial%2F2 - Angular Templates)%3A%20describe%20your%20change...' class='improve-docs btn btn-primary'><i class="glyphicon glyphicon-edit"> </i>Improve this Doc</a> <ul doc-tutorial-nav="2"></ul> <p>Now it's time to make the web page dynamic — with AngularJS. We'll also add a test that verifies the code for the controller we are going to add.</p> <p>There are many ways to structure the code for an application. For Angular apps, we encourage the use of <a href="http://en.wikipedia.org/wiki/Model–View–Controller">the Model-View-Controller (MVC) design pattern</a> to decouple the code and to separate concerns. With that in mind, let's use a little Angular and JavaScript to add model, view, and controller components to our app.</p> <ul> <li>The list of three phones is now generated dynamically from data</li> </ul> <div doc-tutorial-reset="2"></div> <h2 id="view-and-template">View and Template</h2> <p>In Angular, the <strong>view</strong> is a projection of the model through the HTML <strong>template</strong>. This means that whenever the model changes, Angular refreshes the appropriate binding points, which updates the view.</p> <p>The view component is constructed by Angular from this template:</p> <p><strong><code>app/index.html</code>:</strong></p> <pre><code class="lang-html"><html ng-app="phonecatApp"> <head> ... <script src="bower_components/angular/angular.js"></script> <script src="js/controllers.js"></script> </head> <body ng-controller="PhoneListCtrl"> <ul> <li ng-repeat="phone in phones"> <span>{{phone.name}}</span> <p>{{phone.snippet}}</p> </li> </ul> </body> </html> </code></pre> <p>We replaced the hard-coded phone list with the <a href="api/ng/directive/ngRepeat">ngRepeat directive</a> and two <a href="guide/expression">Angular expressions</a>:</p> <ul> <li>The <code>ng-repeat="phone in phones"</code> attribute in the <code><li></code> tag is an Angular repeater directive. The repeater tells Angular to create a <code><li></code> element for each phone in the list using the <code><li></code> tag as the template.</li> <li>The expressions wrapped in curly braces (<code>{{phone.name}}</code> and <code>{{phone.snippet}}</code>) will be replaced by the value of the expressions.</li> </ul> <p>We have added a new directive, called <code>ng-controller</code>, which attaches a <code>PhoneListCtrl</code> <strong>controller</strong> to the <body> tag. At this point:</p> <ul> <li>The expressions in curly braces (<code>{{phone.name}}</code> and <code>{{phone.snippet}}</code> denote bindings, which are referring to our application model, which is set up in our <code>PhoneListCtrl</code> controller.</li> </ul> <div class="alert alert-info"> Note: We have specified an <a href="api/ng/type/angular.Module">Angular Module</a> to load using <code>ng-app="phonecatApp"</code>, where <code>phonecatApp</code> is the name of our module. This module will contain the <code>PhoneListCtrl</code>. </div> <p><img class="diagram" src="img/tutorial/tutorial_02.png"></p> <h2 id="model-and-controller">Model and Controller</h2> <p>The data <strong>model</strong> (a simple array of phones in object literal notation) is now instantiated within the <code>PhoneListCtrl</code> <strong>controller</strong>. The <strong>controller</strong> is simply a constructor function that takes a <code>$scope</code> parameter:</p> <p><strong><code>app/js/controllers.js</code>:</strong></p> <pre><code class="lang-js">var phonecatApp = angular.module('phonecatApp', []); phonecatApp.controller('PhoneListCtrl', function ($scope) { $scope.phones = [ {'name': 'Nexus S', 'snippet': 'Fast just got faster with Nexus S.'}, {'name': 'Motorola XOOM™ with Wi-Fi', 'snippet': 'The Next, Next Generation tablet.'}, {'name': 'MOTOROLA XOOM™', 'snippet': 'The Next, Next Generation tablet.'} ]; }); </code></pre> <p>Here we declared a controller called <code>PhoneListCtrl</code> and registered it in an AngularJS module, <code>phonecatApp</code>. Notice that our <code>ng-app</code> directive (on the <code><html></code> tag) now specifies the <code>phonecatApp</code> module name as the module to load when bootstrapping the Angular application.</p> <p>Although the controller is not yet doing very much, it plays a crucial role. By providing context for our data model, the controller allows us to establish data-binding between the model and the view. We connected the dots between the presentation, data, and logic components as follows:</p> <ul> <li><p>The <a href="api/ng/directive/ngController">ngController</a> directive, located on the <code><body></code> tag, references the name of our controller, <code>PhoneListCtrl</code> (located in the JavaScript file <code>controllers.js</code>).</p> </li> <li><p>The <code>PhoneListCtrl</code> controller attaches the phone data to the <code>$scope</code> that was injected into our controller function. This <em>scope</em> is a prototypical descendant of the <em>root scope</em> that was created when the application was defined. This controller scope is available to all bindings located within the <code><body ng-controller="PhoneListCtrl"></code> tag.</p> </li> </ul> <h3 id="scope">Scope</h3> <p>The concept of a scope in Angular is crucial. A scope can be seen as the glue which allows the template, model and controller to work together. Angular uses scopes, along with the information contained in the template, data model, and controller, to keep models and views separate, but in sync. Any changes made to the model are reflected in the view; any changes that occur in the view are reflected in the model.</p> <p>To learn more about Angular scopes, see the <a href="api/ng/type/$rootScope.Scope">angular scope documentation</a>.</p> <h2 id="tests">Tests</h2> <p>The "Angular way" of separating controller from the view, makes it easy to test code as it is being developed. If our controller is available on the global namespace then we could simply instantiate it with a mock <code>scope</code> object:</p> <pre><code class="lang-js">describe('PhoneListCtrl', function(){ it('should create "phones" model with 3 phones', function() { var scope = {}, ctrl = new PhoneListCtrl(scope); expect(scope.phones.length).toBe(3); }); }); </code></pre> <p>The test instantiates <code>PhoneListCtrl</code> and verifies that the phones array property on the scope contains three records. This example demonstrates how easy it is to create a unit test for code in Angular. Since testing is such a critical part of software development, we make it easy to create tests in Angular so that developers are encouraged to write them.</p> <h3 id="testing-non-global-controllers">Testing non-Global Controllers</h3> <p>In practice, you will not want to have your controller functions in the global namespace. Instead, you can see that we have registered it via an anonymous constructor function on the <code>phonecatApp</code> module.</p> <p>In this case Angular provides a service, <code>$controller</code>, which will retrieve your controller by name. Here is the same test using <code>$controller</code>:</p> <p><strong><code>test/unit/controllersSpec.js</code>:</strong></p> <pre><code class="lang-js">describe('PhoneListCtrl', function(){ beforeEach(module('phonecatApp')); it('should create "phones" model with 3 phones', inject(function($controller) { var scope = {}, ctrl = $controller('PhoneListCtrl', {$scope:scope}); expect(scope.phones.length).toBe(3); })); }); </code></pre> <ul> <li>Before each test we tell Angular to load the <code>phonecatApp</code> module.</li> <li>We ask Angular to <code>inject</code> the <code>$controller</code> service into our test function</li> <li>We use <code>$controller</code> to create an instance of the <code>PhoneListCtrl</code></li> <li>With this instance, we verify that the phones array property on the scope contains three records.</li> </ul> <h3 id="writing-and-running-tests">Writing and Running Tests</h3> <p>Angular developers prefer the syntax of Jasmine's Behavior-driven Development (BDD) framework when writing tests. Although Angular does not require you to use Jasmine, we wrote all of the tests in this tutorial in Jasmine v1.3. You can learn about Jasmine on the <a href="http://jasmine.github.io/">Jasmine home page</a> and at the <a href="http://jasmine.github.io/1.3/introduction.html">Jasmine docs</a>.</p> <p>The angular-seed project is pre-configured to run unit tests using <a href="http://karma-runner.github.io/">Karma</a> but you will need to ensure that Karma and its necessary plugins are installed. You can do this by running <code>npm install</code>.</p> <p>To run the tests, and then watch the files for changes: <code>npm test</code>.</p> <ul> <li>Karma will start a new instance of Chrome browser automatically. Just ignore it and let it run in the background. Karma will use this browser for test execution.</li> <li><p>You should see the following or similar output in the terminal:</p> <pre> info: Karma server started at http://localhost:9876/ info (launcher): Starting browser "Chrome" info (Chrome 22.0): Connected on socket id tPUm9DXcLHtZTKbAEO-n Chrome 22.0: Executed 1 of 1 SUCCESS (0.093 secs / 0.004 secs) </pre> <p>Yay! The test passed! Or not...</p> </li> <li>To rerun the tests, just change any of the source or test .js files. Karma will notice the change and will rerun the tests for you. Now isn't that sweet?</li> </ul> <div class="alert alert-info"> Make sure you don't minimize the browser that Karma opened. On some OS, memory assigned to a minimized browser is limited, which results in your karma tests running extremely slow. </div> <h1 id="experiments">Experiments</h1> <ul> <li><p>Add another binding to <code>index.html</code>. For example:</p> <pre><code class="lang-html"><p>Total number of phones: {{phones.length}}</p> </code></pre> </li> <li><p>Create a new model property in the controller and bind to it from the template. For example:</p> <pre><code>$scope.name = "World"; </code></pre> <p>Then add a new binding to <code>index.html</code>:</p> <pre><code><p>Hello, {{name}}!</p> </code></pre> <p>Refresh your browser and verify that it says "Hello, World!".</p> </li> <li><p>Update the unit test for the controller in <code>./test/unit/controllersSpec.js</code> to reflect the previous change. For example by adding:</p> <pre><code>expect(scope.name).toBe('World'); </code></pre> </li> <li><p>Create a repeater in <code>index.html</code> that constructs a simple table:</p> <pre><code><table> <tr><th>row number</th></tr> <tr ng-repeat="i in [0, 1, 2, 3, 4, 5, 6, 7]"><td>{{i}}</td></tr> </table> </code></pre> <p>Now, make the list 1-based by incrementing <code>i</code> by one in the binding:</p> <pre><code><table> <tr><th>row number</th></tr> <tr ng-repeat="i in [0, 1, 2, 3, 4, 5, 6, 7]"><td>{{i+1}}</td></tr> </table> </code></pre> <p>Extra points: try and make an 8x8 table using an additional <code>ng-repeat</code>.</p> </li> <li><p>Make the unit test fail by changing <code>expect(scope.phones.length).toBe(3)</code> to instead use <code>toBe(4)</code>.</p> </li> </ul> <h1 id="summary">Summary</h1> <p>You now have a dynamic app that features separate model, view, and controller components, and you are testing as you go. Now, let's go to <a href="tutorial/step_03">step 3</a> to learn how to add full text search to the app.</p> <ul doc-tutorial-nav="2"></ul>