javascript代码实例教程-AngularJS学习--- AngularJS中数据双向绑定(two-way data-binding) orderBy step4

发布时间:2019-02-02 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-AngularJS学习--- AngularJS中数据双向绑定(two-way data-binding) orderBy step4脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 1.切换工作目录

gIT checkout step-4  #切换分支,切换到第4步

npm start  #启动项

2.代码

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>

  </li>

</ul>

复制代码

app/controllers.js

 

复制代码

VAR phonecatApp = Angular.module('phonecatApp', []);

 

phonecatApp.controller('PhoneListCtrl', function($scope) {

  $scope.phones = [

    {'name': 'Nexus S',

     'snippet': 'Fast just got faster with Nexus S.',

     'age': 1},

    {'name': ';motorola XOOM™ with Wi-Fi',

     'snippet': 'The Next, Next Generation tablet.',

     'age': 2},

    {'name': 'MOTOROLA XOOM™',

     'snippet': 'The Next, Next Generation tablet.',

     'age': 3}

  ];

 

  $scope.orderProp = 'age';

});

复制代码

 

 

 

 

3.效果

按字母排序:

 

 

 

 

 

按时间排序:

 

 

 

很明显,相较于step-3,step-4新增加了排序功能

 

 

 

4.原理说明

 

 

首先,添加了<select> 标签:

 

<select ng-model="orderProp">

  <option value="name">Alphabetical</option>

  <option value="age">Newest</option>

</select>

其次,在filter中添加了orderBy:

 

 <li ng-repeat="phone in phones | filter:query | orderBy:orderProp">

    {{phone.name}}

    <p>{{phone.snippet}}</p>

  </li>

所以,根据angularjs的思想,一是model改变(可能是用户手动选择下拉框导致的),那么根据数据绑定原则(data-binding),view也将适时进行改变.

 

orderBy api:https://docs.angularjs.org/api/ng/filter/orderBy

 

orderBy usage(用法)

In HTML Template Binding(在HTML中的用法)

{{ orderBy_exPression | orderBy : expression : reverse}}

In JavaScript(在JS中的用法)

$filter('orderBy')(array, expression, reverse)

上面的例子是在HTML中用的,默认string类型的数据是按照字母表中数据排序的,而number数字类型的数据是按照数字大小进行排序的.

 

如果想要倒序,那么可以在上面的option value='-name',加上一个'-'即可.

 

 

 

5.测试

复制代码

amosli@amosli-pc:~/develop/angular-phonecat$ npm run protractor 

 

> angular-phonecat@0.0.0 preprotractor /home/amosli/develop/angular-phonecat

> npm run update-webdriver

 

 

> angular-phonecat@0.0.0 preupdate-webdriver /home/amosli/develop/angular-phonecat

> npm install

 

 

> angular-phonecat@0.0.0 postinstall /home/amosli/develop/angular-phonecat

> bower install

 

 

> angular-phonecat@0.0.0 update-webdriver /home/amosli/develop/angular-phonecat

> webdriver-manager update

 

selenium standalone is up to date.

chromedriver is up to date.

 

> angular-phonecat@0.0.0 protractor /home/amosli/develop/angular-phonecat

> protractor test/protractor-conf.js

 

 

------------------------------------

PID: 5265 (capability: chrome #1)

------------------------------------

 

Using ChromeDriver directly...

..

 

Finished in 5.033 seconds

2 tests, 5 assertions, 0 failures

复制代码

这里执行的是端到端的测试,测试代码如下:

 

angular-phonecat/test/e2e/scenarios.js

 

amosli@amosli-pc:~/develop/angular-phonecat/test/e2e$ cat scenarios.js 

'use strict';

 

/* https://docs.angularjs.org/guide/dev_guide.e2e-testing */

 

describe('PhoneCat App', function() {

 

  describe('Phone list view', function() {

 

    beforeeach(function() {

      browser.get('app/index.html');

    });

 

 

    it('should filter the phone list as user types into the search box', function() {

 

      var phoneList = element.all(by.repeater('phone in phones'));

      var query = element(by.model('query'));

 

      expect(phoneList.count()).toBe(3);

 

      query.sendKeys('nexus');

      expect(phoneList.count()).toBe(1);

 

      query.clear();

      query.sendKeys('motorola');

      expect(phoneList.count()).toBe(2);

    });

 

 

    it('should be possible to control phone order via the drop down select box', function() {

 

      var phoneNameColumn = element.all(by.repeater('phone in phones').column('{{phone.name}}'));

      var query = element(by.model('query'));

 

      function getnames() {

        return phoneNameColumn.map(function(elm) {

          return elm.getText();

        });

      }

 

      query.sendKeys('tablet'); //let's narrow the dataset to make the test assertions shorter

 

      expect(getNames()).toEqual([

        "Motorola XOOM/u2122 with Wi-Fi",

        "MOTOROLA XOOM/u2122"

      ]);

 

      element(by.model('orderProp')).findElement(by.css('option[value="name"]')).click();

 

      expect(getNames()).toEqual([

        "MOTOROLA XOOM/u2122",

        "Motorola XOOM/u2122 with Wi-Fi"

      ]);

    });

  });

});

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-AngularJS学习--- AngularJS中数据双向绑定(two-way data-binding) orderBy step4全部内容,希望文章能够帮你解决javascript代码实例教程-AngularJS学习--- AngularJS中数据双向绑定(two-way data-binding) orderBy step4所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。