AngularJS – Directive & Filter 介紹篇

為了方便文章閱讀,本篇將filter翻譯成過濾器、directive翻譯成指示指令

在看過前幾篇的AngularJS的教學後,我們對AngularJS的開發應該有了基本的認識,針對HTML文件中我們的操作有:

針對頁面引入應用程式模組

<html ng-app="store">

針對頁面引入控制器

<body ng-controller="StoreController as store">

針對頁面去顯示或隱藏元素

<h1 ng-show="name">Hello, {{ name }} !</h1>

針對大量的資料設定重複的區域

<li ng-repeat="product in store.products">{{ product.name }}</li>
AngularJS Part4 Slide1

接下來針對過濾器來介紹,依下圖所示產品價格的部分,有的是有小數點有的沒有,會造成頁面在資料呈現上的參差不齊。

AngularJS Part4 Slide2

若我們在該表達式(expression)加上一條直線(pipe),在直線後面可以加入一些過濾器,本範例加入的是貨幣格式的過濾器,當加入後價格在頁面上顯示的格式,就會自動加上了$符號和兩位數的小數點。

AngularJS Part4 Slide3

我們也可以在過濾器後面利用冒號再加上一些選項,讓呈現的資料更不同,例如下圖中date格式中是以「月/日/年 @ 時:分AM」這樣的方式顯示,而除了貨幣currency、日期date的過濾器之外,還包含有大小寫轉換的uppercase、lowercase、字數也可以當成資料呈現筆數的limitTo、另外還有可以針對重複性的資料進行排序的orderBy,大概的使用方式可以參考下圖,或是在JS Bin看看馬老師製作的案例。

AngularJS Part4 Slide4

另外關於指示指令部分,在這次的案例上我們加上產品的圖片,圖片的部分有兩種,一個是大圖;另一個是產品的縮圖,也因為有兩種不同的資料,要注意他被加在數列裡面的方式。

AngularJS Part4 Slide5

在頁面上顯示圖片的方式要注意不是用src的屬性去呈現,而是需要用ng-src這個屬性,加上圖片欄位的表達式即可,在JS Bin上面觀賞本案例程式碼。

AngularJS Part4 Slide6

※以上截圖部分為AngularJS官方教學視訊,另有filter指令介紹的專頁和所有filter清單

You may also like...

14,741 Responses

  1. AnthonyPress表示:

    my canadian pharmacy canadian pharmacy no scripts canadian drug

  2. Trefqtg表示:

    Здравствуйте!
    Как приобрести диплом о среднем образовании в Москве и других городах
    marandola.net/dokuwiki/doku.php?id=premialniediploms24
    Окажем помощь!.

  3. Eanrxvh表示:

    Здравствуйте!
    Мы изготавливаем дипломы любых профессий по приятным тарифам.
    pedlamrisk.com/page4.htm

  4. JeremyPsymn表示:

    https://easyrxindia.shop/# india pharmacy

  5. Mazrmgb表示:

    Привет, друзья!
    Как быстро и легально купить аттестат 11 класса в Москве
    balipraia.com/tag/balipraia

  6. Trefzps表示:

    Здравствуйте!
    Приобретение диплома ПТУ с сокращенной программой обучения в Москве
    старый.ркэ.рф/index.php/component/k2/itemlist/user/127651
    Рады оказать помощь!.

  7. Dnrthqi表示:

    Добрый день!
    Приобрести документ о получении высшего образования можно в нашей компании в Москве.
    hnfmrfdtg.gallery.ru

  8. WilliamDeeli表示:

    Привет, друзья!
    Мы изготавливаем дипломы.
    odessaflower.ukrbb.net/viewtopic.php?f=55&t=25568

  9. Hermanswoff表示:

    best online pharmacy india: online shopping pharmacy india – pharmacy website india

  10. Sazrawz表示:

    Привет!
    Диплом о высшем образовании
    telegra.ph/kupit-diplom-belarus-08-22-2

  11. Sazruhh表示:

    Добрый день!
    Диплом для вас
    mediamemorial.ru/club/user/125807/forum/message/3881/13363/#message13363

  12. Cazrbee表示:

    Привет!
    Заказать диплом о высшем образовании
    ninokuni.ru/member.php?action=profile&uid=3583

  13. Peterhew表示:

    п»їlegitimate online pharmacies india: pharmacy website india – pharmacy website india

  14. Hi there! This post couldn’t be written any better! Reading this post reminds me of my previous room mate! He always kept chatting about this. I will forward this article to him. Fairly certain he will have a good read. Many thanks for sharing!

  15. Hermanswoff表示:

    mexican pharmaceuticals online: mexican online pharmacies prescription drugs – mexico drug stores pharmacies

  16. Hermanswoff表示:

    top online pharmacy india: pharmacy website india – online shopping pharmacy india

  17. AnthonyPress表示:

    cross border pharmacy canada vipps approved canadian online pharmacy canadian pharmacy 24 com

  18. Cazrbrq表示:

    Здравствуйте!
    Приобрести диплом о высшем образовании
    polkasocial.org/read-blog/27296?mode=day

  19. Peterhew表示:

    mexican mail order pharmacies: п»їbest mexican online pharmacies – mexican rx online

  20. Robertengep表示:

    https://easyrxcanada.com/# safe online pharmacies in canada

  21. AnthonyPress表示:

    canadian compounding pharmacy canadianpharmacy com best rated canadian pharmacy

  22. Robertengep表示:

    https://easyrxindia.shop/# best india pharmacy

  23. AnthonyPress表示:

    canadian online drugstore canadian pharmacy online canadian pharmacy cheap

  24. Hermanswoff表示:

    top online pharmacy india: online pharmacy india – buy prescription drugs from india

  25. Sazrbuy表示:

    Добрый день!
    Диплом любой специальности
    blog.nataraj.ru/~/Interest

  26. Mazrvvq表示:

    Добрый день!
    Пошаговая инструкция по официальной покупке диплома о высшем образовании
    phonotope.net/topics/archives/000052

  27. Dnrtkud表示:

    Привет!
    Купить документ о получении высшего образования вы имеете возможность в нашей компании в Москве.
    bestallorder.nnov.org/blog/predpochitaete_kupit_originalnyy_diplom_

  28. I just could not go away your site before suggesting that I extremely enjoyed the usual info an individual provide for your guests? Is gonna be again ceaselessly in order to check up on new posts

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。