AngularJS學習--- AngularJS中資料雙向繫結(two-way data-binding) orderBy step4

來源:互聯網
上載者:User

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 seconds2 tests, 5 assertions, 0 failures複製代碼這裡執行的是端到端的測試,測試代碼如下: angular-phonecat/test/e2e/scenarios.js amosli@amosli-pc:~/develop/angular-phonecat/test/e2e$ cat scenarios.js 'use strict'; /* http://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"      ]);    });  });});

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.