AngularJS步驟03---05

步驟3——把模板添加到組件

步驟3——把模板添加到組件

工作區(qū)切換到步驟3

git checkout -f step-3

app/index.html

<!doctype html>
<html lang="en" ng-app="phonecatApp">
  <head>
    <meta charset="utf-8">
    <title>Google Phone Gallery</title>
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" />
    <link rel="stylesheet" href="app.css" />
    <script src="bower_components/angular/angular.js"></script>
    <script src="app.js"></script>
    <script src="phone-list.component.js"></script>
  </head>
  <body>

    <!-- Use a custom component to render a list of phones -->
    <phone-list></phone-list>

  </body>
</html>

我們繼續(xù)分析一下上面的代碼

1、引入組件文件

<script src="phone-list.component.js"></script>

2、在body里添加組件

 <phone-list></phone-list>

讓我們看一下phone-list.component.js里面到底是怎么編寫的

'use strict';

// Register `phoneList` component, along with its associated controller and template
angular.
  module('phonecatApp').
  component('phoneList', {
    template:
        '<ul>' +
          '<li ng-repeat="phone in $ctrl.phones">' +
            '<span>{{phone.name}}</span>' +
            '<p>{{phone.snippet}}</p>' +
          '</li>' +
        '</ul>',
    controller: function PhoneListController() {
      this.phones = [
        {
          name: 'Nexus S',
          snippet: 'Fast just got faster with Nexus S.'
        }, {
          name: 'Motorola XOOM? with Wi-Fi',
          snippet: 'The Next, Next Generation tablet.'
        }, {
          name: 'MOTOROLA XOOM?',
          snippet: 'The Next, Next Generation tablet.'
        }
      ];
    }
  });

工作區(qū)切換到步驟4

index.html

<!doctype html>
<html lang="en" ng-app="phonecatApp">
  <head>
    <meta charset="utf-8">
    <title>Google Phone Gallery</title>
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" />
    <link rel="stylesheet" href="app.css" />
    <script src="bower_components/angular/angular.js"></script>
    <script src="app.module.js"></script>
    <script src="phone-list/phone-list.module.js"></script>
    <script src="phone-list/phone-list.component.js"></script>
  </head>
  <body>

    <!-- Use a custom component to render a list of phones -->
    <phone-list></phone-list>

  </body>
</html>

app.module.js

'use strict';

// Define the `phonecatApp` module
angular.module('phonecatApp', [
  // ...which depends on the `phoneList` module
  'phoneList'
]);

工作區(qū)切換到步驟5——過濾轉(zhuǎn)換器

我們在上一步我們做了大量的工作,為后面打下了良好基礎(chǔ)。所以現(xiàn)在我們可以簡單的擴(kuò)展就添加一個全文搜索的功能(是的,這很簡單!)。我們也會寫一個端到端(end-to-end,E2E)測試,因?yàn)榱己玫亩说蕉藴y試是應(yīng)用的好朋友,就像有一雙眼睛時刻看著應(yīng)用,快速定位故障。

程序會有一個搜索框。你需要注意在搜索框輸入時電話列表的變化。

模板

這里我們將引入phone-list.template.html模板

<div class="container-fluid">
  <div class="row">
    <div class="col-md-2">
      <!--Sidebar content-->

      Search: <input ng-model="$ctrl.query" />

    </div>
    <div class="col-md-10">
      <!--Body content-->

      <ul class="phones">
        <li ng-repeat="phone in $ctrl.phones | filter:$ctrl.query">
          <span>{{phone.name}}</span>
          <p>{{phone.snippet}}</p>
        </li>
      </ul>

    </div>
  </div>
</div>

我們添加了一個標(biāo)準(zhǔn)的HTML<input>標(biāo)簽,并對ngRepeat指令應(yīng)用了Angular的filter功能來處理<code><input></code>輸入。

就是這些改變讓用戶輸入搜索條件后馬上可以在手機(jī)列表中看到對應(yīng)的變化。這些新代碼展示了:

數(shù)據(jù)綁定:這一Angular核心特性。當(dāng)頁面加載后,Angular把input標(biāo)簽定義的輸入框的值綁定到一個命名變量中(頁面中命名為query的變量),這個變量還以相同的名字存在于數(shù)據(jù)模型中,二者是完全同步的。在這個代碼中輸入框鍵入的內(nèi)容(綁定到query)會立即作為列表輸出時的過濾條件 (通過phone in phones | filter:query 這一句)。數(shù)據(jù)模型的變化導(dǎo)致轉(zhuǎn)換器的輸入改變,轉(zhuǎn)換器通過更新DOM來反映模型的當(dāng)前狀態(tài)。

tutorial_03.png

使用filter過濾器:filter功能使用query來創(chuàng)建一個新的數(shù)組(只有那些記錄中匹配了query對應(yīng)值),ngRepeat自動根據(jù)附加了filter變動的手機(jī)列表數(shù)據(jù)。這在開發(fā)過程中是完全透明的。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Angular應(yīng)用程序啟動 ng-app指令 指令指定Angular應(yīng)用程序的根(root)元素,用于啟動Angu...
    angelwgh閱讀 530評論 0 1
  • 導(dǎo)言 最近在學(xué)AngularJS的實(shí)例教程PhoneCat Tutorial App,發(fā)現(xiàn)網(wǎng)上的中文教程都比較久遠(yuǎn)...
    minxuan閱讀 5,058評論 8 27
  • 導(dǎo)言 最近在學(xué)AngularJS的實(shí)例教程PhoneCat Tutorial App,發(fā)現(xiàn)網(wǎng)上的中文教程都比較久遠(yuǎn)...
    minxuan閱讀 1,928評論 5 8
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,308評論 25 708
  • 還有最后兩周就要離職,內(nèi)心忐忑不安,各種心潮澎湃,這么多想做的事情從哪里開始好呢?萬一被職場拋棄淪為黃臉婆我該如何...
    LindaLou閱讀 587評論 1 2

友情鏈接更多精彩內(nèi)容