Angular series----Angularjs Getting Started Tutorial 05: Two-way binding (reprint)

Source: Internet
Author: User

In this step you will add a feature that allows the user to control the display order of the phone list. Dynamic sorting can be implemented by adding a new model property, integrating it with an iterator, and then having the data bound to complete the rest.

Please reset your working directory:

git checkout -f step-4

You should find that in addition to the search box, your app has a more down menu, which allows you to control the order in which your phone is arranged.

The most important differences between steps 3 and 4 are listed below. You can see the whole difference on GitHub.

Template

App/index.html

Search: <Input ng-Model="Query">Sort By:<Select Ng-model= "Orderprop" > <option value=  "name" >alphabetical< /option> <option value= "age" >newest</option ></select><ul class= "Phones" > <li ng-repeat= "Phone in phones | Filter:query | Orderby:orderprop "> {phone.name}} <p>{{phone.snippet}}</p>< Span class= "PLN" > </li></ul>  

We made the following changes in index.html :

    • First, we add a tag called orderProp <select> , so that our users can choose the two sort methods we provide.

    • Then, after the filter filter, add an order by filter to process the data into the iterator. The orderBy filter takes an array as input, copies a copy, and then re-sorts the copy and then outputs it to the iterator.

Angularjs select orderProp creates a two-way binding between the element and the model. orderPropIt is then used as orderBy input to the filter.

As we talked about data binding and iterators in step 3, whenever the data model changes (such as when the user chooses a different order in the drop-down menu), the ANGULARJS data binding causes the view to update automatically. No clumsy DOM operation!

Controller

App/js/controllers.js:

function Phonelistctrl($scope) {$scope.Phones= [ {"Name": "Nexus S", "Snippet": "Fast just got faster with Nexus S.", "Age": 0}, {"Name": "Motorola xoom™with Wi-Fi", "Snippet": "The next, next Generation tablet.",  "age" : 1},  { "name" :  " MOTOROLA xoom™ ", " snippet ":   "the next, next Generation tablet." ,  "age" :  2} ]; $scope .=  ' age ' ;               /span>                 
    • We have modified the phones model-the array of phones-to add an attribute to each cell phone record age . We age sort the phones according to the attributes.
    • We added a line to the controller code to let orderProp the default value be age . If we do not set the default value, the model will remain uninitialized until our users select a sequence in the drop-down menu.

      Now we should talk about bidirectional 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 the ' age ' in the controller orderProp . So binding works in the direction from our model to the user interface-the binding of data from model to view. Now when you select "Alphabetically" in the drop-down menu, the data model is updated at the same time, and the cell list array is reordered. This time data binding has a role in another direction-the binding of data from view to model.

Test

The changes we make can be validated by a unit test or an end-to-end test. Let's take a look at the unit tests first:

Test/unit/controllersspec.js:

Describe(' Phonecat controllers ', function() {Describe(' Phonelistctrl ', function(){ VarScope,Ctrl;Beforeeach(function() {Scope= {},Ctrl= New Phonelistctrl(Scope); });It(' should create ' phones ' model with 3 phones ', function() {Expect(Scope.Phoneslength). tobe (3 It ( ' should set the default value of Orderprop model ' , function ()  {< Span class= "PLN" > Expect (scope. Orderproptobe ( }); });             

Unit tests now verify that the default values are set correctly.

We use the Jasmine interface to PhoneListCtrl extract the controller into a beforeEach block that is describe shared by all the tests in all the parent blocks.

Run these unit tests, as before, execute the ./scripts/test.sh script, you should see the following output ( Note: to open http://localhost:9876 in the browser and enter strict mode, the test will not run!) ):

Chrome: RunnerReset...Total 2Tests(Passed: 2; Fails: 0; Errors: 0) (3.00Ms) chrome 19.0. 1084.36 mac Os: run 2 tests  ( passed: 2; fails: 0; errors 0)  (3.00 Ms)       

Now let's shift our attention to end-to-end testing.

Test/e2e/scenarios.js:

...It(' Should is possible to control phone order via the drop-down select box ', function() { Let's narrow the dataset to make the test assertions shorterInput(' Query ').Enter(' Tablet ');Expect(Repeater('. Phones Li ', ' Phone List ').Column(' Phone.name ')).Toequal(["Motorola xoom\u2122 with Wi-Fi", "MOTOROLA xoom\u2122"]); select ( ' orderprop '  Option ' alphabetical '  expect repeater ( phones Li ' , ).  Column ' phone.name '  toequal  ([ "MOTOROLA xoom\u2122" ,  "Motorola xoom\u2122 with Wi-Fi" );  });  ...             

The end-to-end test verifies that the sorting mechanism of the option box is correct.

You can now refresh your browser and run the end-to-end test again, or you can run it on the ANGULARJS server.

Practice
    • In the PhoneListCtrl controller, delete the SET orderProp statement, and you will see that ANGULARJS will temporarily add a blank option in the drop-down menu, and the sort order is the default sort (that is, unsorted).
    • index.htmlAdd a ' {{Orderprop}} binding to the template to display its value in real time.
Summarize

Now that you've provided search functionality for your app, it's fully tested. Step 5 We will learn ANGULARJS services and how Angularjs uses dependency injection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.