[Angular Tutorial] 3-filtering repeaters

Source: Internet
Author: User

In the previous step, we took a lot of effort to lay the groundwork for the application, so let's do something simple now! We'll add a full-text search box (yes, it's simple).

• Our app now has a search box, notice that the changes in the phone list on the page depend on what the user typed in the search box.

The most important changes are listed below, of course, you can also click here to view all the differences on GitHub.

Controller

We do not make any changes to the controller.

Template

app/index.html:

<div class= "Container-fluid" >  <div class= "Row" >    <div class= "Col-md-2" >      <!-- Sidebar content-->      Search: <input ng-model= "Query" >    </div>    <div class= "col-md-10" >      <!--Body content-->      <ul class= "Phones" >        <li ng-repeat= "Phone in phones | filter:query >          {{phone.name}}          <p>{{phone.snippet}}</p>        </li>      </ul>    < /div>  </div></div>

We added the standard html<input> tag and used the angular filter function to process the input for the Ngrepeat directive.

This allows the user to enter search criteria in the phone list and to see the search results in a timely manner, and the new code shows:

• Data binding, which is one of the core features of angular. When the page loads, angular binds the name of the input box to a variable with the same name in the data model and keeps the two synchronized.

In this code, the data entered by the user in the input box (called query) can immediately be used as a filter in a list iterator ( phone in phones | filter: query ). When changes in the data model lead to changes in the iterator input, the iterator efficiently updates the DOM to reflect the state of the current data model.

Use of the filter filter: The filter function uses the value of query to create a list of worthy phones that contain only those matching the query.

• The ngrepeat automatically updates the view by changing the number of calls returned by the filter filter, which is completely transparent to the developer.

Summarize

We've added a full-text search, so let's go to the next step to learn how to add sorting to the phone app!

[Angular Tutorial] 3-filtering repeaters

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.