Improve this doc

In this step, you will add a feature to let your users control the order of the items in the phone list. The dynamic ordering is implemented by creating a new model property, wiring it together with the repeater, and letting the data binding magic do the rest of the work.

You should see that in addition to the search box, the app displays a drop down menu that allows users to control the order in which the phones are listed.

The most important differences between Steps 3 and 4 are listed below. You can see the full diff on GitHub:

Template

app/index.html:

  1. Search: <input ng-model="query">
  2. Sort by:
  3. <select ng-model="orderProp">
  4. <option value="name">Alphabetical</option>
  5. <option value="age">Newest</option>
  6. </select>
  7.  
  8.  
  9. <ul class="phones">
  10. <li ng-repeat="phone in phones | filter:query | orderBy:orderProp">
  11. {{phone.name}}
  12. <p>{{phone.snippet}}</p>
  13. </li>
  14. </ul>

We made the following changes to the index.html template:

  • First, we added a html element named orderProp, so that our users can pick from the two provided sorting options.

Experiments - 图1

  • We then chained the filter filter with orderBy filter to further process the input into the repeater. orderBy is a filter that takes an input array, copies it and reorders the copy which is then returned.

Angular creates a two way data-binding between the select element and the orderProp model. orderProp is then used as the input for the orderBy filter.

As we discussed in the section about data-binding and the repeater in step 3, whenever the model changes (for example because a user changes the order with the select drop down menu), Angular's data-binding will cause the view to automatically update. No bloated DOM manipulation code is necessary!

Controller

app/js/controllers.js:

  1. var phonecatApp = angular.module('phonecatApp', []);
  2.  
  3. phonecatApp.controller('PhoneListCtrl', function ($scope) {
  4. $scope.phones = [
  5. {'name': 'Nexus S',
  6. 'snippet': 'Fast just got faster with Nexus S.',
  7. 'age': 1},
  8. {'name': 'Motorola XOOM™ with Wi-Fi',
  9. 'snippet': 'The Next, Next Generation tablet.',
  10. 'age': 2},
  11. {'name': 'MOTOROLA XOOM™',
  12. 'snippet': 'The Next, Next Generation tablet.',
  13. 'age': 3}
  14. ];
  15.  
  16. $scope.orderProp = 'age';
  17. });
  • We modified the phones model - the array of phones - and added an age property to each phone record. This property is used to order phones by age.

  • We added a line to the controller that sets the default value of orderProp to age. If we had not set the default value here, the model would stay uninitialized until our user would pick an option from the drop down menu.

This is a good time to talk about two-way data-binding. Notice that when the app is loaded in the browser, "Newest" is selected in the drop down menu. This is because we set orderProp to 'age' in the controller. So the binding works in the direction from our model to the UI. Now if you select "Alphabetically" in the drop down menu, the model will be updated as well and the phones will be reordered. That is the data-binding doing its job in the opposite direction — from the UI to the model.

Test

The changes we made should be verified with both a unit test and an end-to-end test. Let's look at the unit test first.

test/unit/controllersSpec.js:

  1. describe('PhoneCat controllers', function() {
  2.  
  3. describe('PhoneListCtrl', function(){
  4. var scope, ctrl;
  5.  
  6. beforeEach(module('phonecatApp'));
  7.  
  8. beforeEach(inject(function($controller) {
  9. scope = {};
  10. ctrl = $controller('PhoneListCtrl', {$scope:scope});
  11. }));
  12.  
  13. it('should create "phones" model with 3 phones', function() {
  14. expect(scope.phones.length).toBe(3);
  15. });
  16.  
  17.  
  18. it('should set the default value of orderProp model', function() {
  19. expect(scope.orderProp).toBe('age');
  20. });
  21. });
  22. });

The unit test now verifies that the default ordering property is set.

We used Jasmine's API to extract the controller construction into a beforeEach block, which is shared by all tests in the parent describe block.

You should now see the following output in the Karma tab:

  1. Chrome 22.0: Executed 2 of 2 SUCCESS (0.021 secs / 0.001 secs)

Let's turn our attention to the end-to-end test.

test/e2e/scenarios.js:

  1. ...
  2. it('should be possible to control phone order via the drop down select box',
  3. function() {
  4. //let's narrow the dataset to make the test assertions shorter
  5. input('query').enter('tablet');
  6.  
  7. expect(repeater('.phones li', 'Phone List').column('phone.name')).
  8. toEqual(["Motorola XOOM\u2122 with Wi-Fi",
  9. "MOTOROLA XOOM\u2122"]);
  10.  
  11. select('orderProp').option('Alphabetical');
  12.  
  13. expect(repeater('.phones li', 'Phone List').column('phone.name')).
  14. toEqual(["MOTOROLA XOOM\u2122",
  15. "Motorola XOOM\u2122 with Wi-Fi"]);
  16. });
  17. ...

The end-to-end test verifies that the ordering mechanism of the select box is working correctly.

You can now rerun ./scripts/e2e-test.sh or refresh the browser tab with the end-to-end test runner.html to see the tests run, or you can see them running on Angular's server.

Experiments

  • In the PhoneListCtrl controller, remove the statement that sets the orderProp value and you'll see that Angular will temporarily add a new "unknown" option to the drop-down list and the ordering will default to unordered/natural order.

  • Add an {{orderProp}} binding into the index.html template to display its current value as text.

  • Reverse the sort order by adding a - symbol before the sorting value:

Summary

Now that you have added list sorting and tested the app, go to step 5 to learn about Angular services and how Angular uses dependency injection.