博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
5 - Filtering Repeaters
阅读量:6389 次
发布时间:2019-06-23

本文共 4808 字,大约阅读时间需要 16 分钟。

hot3.png

We did a lot of work in laying a foundation for the app in the previous steps, so now we'll do something simple; we will add full-text search (yes, it will be simple!). We will also write an end-to-end (E2E) test, because a good E2E test is a good friend. It stays with your app, keeps an eye on it, and quickly detects regressions.

  • The app now has a search box. Notice that the phone list on the page changes depending on what a user types into the search box.

Workspace Reset Instructions ➤

The most important changes are listed below. You can see the full diff on .

Component Controller

We made no changes to the component's controller.

Component Template

app/phone-list/phone-list.template.html:

Search:
  • { {phone.name}}

    { {phone.snippet}}

We added a standard HTML <input> tag and used Angular's  function to process the input for the  directive.

By virtue of the  directive, this lets a user enter search criteria and immediately see the effects of their search on the phone list. This new code demonstrates the following:

  • Data-binding: This is one of the core features in Angular. When the page loads, Angular binds the value of the input box to the data model variable specified with ngModel and keeps the two in sync.

    In this code, the data that a user types into the input box (bound to $ctrl.query) is immediately available as a filter input in the list repeater (phone in $ctrl.phones | filter:$ctrl.query). When changes to the data model cause the repeater's input to change, the repeater efficiently updates the DOM to reflect the current state of the model.

    tutorial_05.png

  • Use of the filter filter: The  function uses the $ctrl.query value to create a new array that contains only those records that match the query.

    ngRepeat automatically updates the view in response to the changing number of phones returned by the filter filter. The process is completely transparent to the developer.

Testing

In previous steps, we learned how to write and run unit tests. Unit tests are perfect for testing controllers and other parts of our application written in JavaScript, but they can't easily test templates, DOM manipulation or interoperability of components and services. For these, an end-to-end (E2E) test is a much better choice.

The search feature was fully implemented via templates and data-binding, so we'll write our first E2E test, to verify that the feature works.

e2e-tests/scenarios.js:

describe('PhoneCat Application', function() {  describe('phoneList', function() {    beforeEach(function() {      browser.get('index.html');    });    it('should filter the phone list as a user types into the search box', function() {      var phoneList = element.all(by.repeater('phone in $ctrl.phones'));      var query = element(by.model('$ctrl.query'));      expect(phoneList.count()).toBe(3);      query.sendKeys('nexus');      expect(phoneList.count()).toBe(1);      query.clear();      query.sendKeys('motorola');      expect(phoneList.count()).toBe(2);    });  });});

This test verifies that the search box and the repeater are correctly wired together. Notice how easy it is to write E2E tests in Angular. Although this example is for a simple test, it really is that easy to set up any functional, readable, E2E test.

Running E2E Tests with Protractor

Even though the syntax of this test looks very much like our controller unit test written with Jasmine, the E2E test uses APIs of . Read about the Protractor APIs in the .

Much like Karma is the test runner for unit tests, we use Protractor to run E2E tests. Try it with npm run protractor. E2E tests take time, so unlike with unit tests, Protractor will exit after the tests run and will not automatically rerun the test suite on every file change. To rerun the test suite, execute npm run protractor again.

Note: In order for protractor to access and run tests against your application, it must be served via a web server. In a different terminal/command line window, run npm start to fire up the web server.

Experiments

  • Display the current value of the query model by adding a {

    {$ctrl.query}} binding into the phone-list.template.html template and see how it changes, when you type in the input box.

    You might also try to add the {

    {$ctrl.query}} binding to index.html. However, when you reload the page, you won't see the expected result. This is because the query model lives in the scope defined by the <phone-list> component.
    Component isolation at work!

Summary

We have now added full-text search and included a test to verify that it works! Now let's go on to  to learn how to add sorting capabilities to the PhoneCat application.

转载于:https://my.oschina.net/changeme/blog/805755

你可能感兴趣的文章
cocos2dx-2.x CCFileUtils文件管理分析(2)
查看>>
Emacs中多个golang项目的配置方法
查看>>
未知宽高div水平垂直居中3种方法
查看>>
Vim替换查找
查看>>
如何用sysbench做好IO性能测试
查看>>
利用线性回归模型进行卫星轨道预报
查看>>
懒加载和预加载
查看>>
前端面试题
查看>>
Python的赋值、浅拷贝、深拷贝
查看>>
用python操作mysql数据库(之代码归类)
查看>>
centos安装iftop监控服务器流量
查看>>
ArcGIS Server 10.1 SP1连续查询出现Unable to complete operation错误
查看>>
执行./configure报checking for g++... no错误
查看>>
Dojo学习笔记(十一):Dojo布局——嵌套样例
查看>>
Appium for Android元素定位方法
查看>>
pfSense LAGG(链路聚合)设置
查看>>
教学思路SQL之入门习题《学生成绩》 七.存储过程基础知识
查看>>
createrepo 无法使用解决
查看>>
.net安全类库
查看>>
在Windows 2008 R2上部署SCCM 2007 R2
查看>>