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,634 Responses

  1. Stanleyinolf表示:

    buy lipitor online uk buy lipitor 20mg lipitor for sale

  2. DanielTet表示:

    buy cytotec online http://tamoxifen.bid/# nolvadex for sale amazon
    lasix 100mg

  3. Trefyli表示:

    Здравствуйте!
    Сколько стоит диплом высшего и среднего образования и как это происходит?
    buhtarma.ru/users/evyzafyr
    Окажем помощь!.

  4. After I originally commented I clicked the -Notify me when new comments are added- checkbox and now every time a comment is added I get 4 emails with the identical comment. Is there any method you possibly can take away me from that service? Thanks!

  5. Sazryem表示:

    Привет, друзья!
    Мы готовы предложить дипломы любой профессии.
    Покупка документа, который подтверждает обучение в университете, – это выгодное решение.
    eythar.org/blog/index.php?entryid=652300
    Рады помочь!.

  6. Diplomi_bdEa表示:

    Привет!
    Купить документ о получении высшего образования вы можете в нашей компании в Москве.
    ntsr.info/forum/user/97221

  7. Yrefsag表示:

    Привет!
    Заказать диплом любого ВУЗа.
    sskyn.com/home-uid-84291

  8. One thing I would like to say is the fact that car insurance termination is a horrible experience so if you’re doing the correct things as being a driver you simply won’t get one. A number of people do obtain notice that they are officially dumped by their insurance company they then have to struggle to get extra insurance after the cancellation. Cheap auto insurance rates are generally hard to get from a cancellation. Knowing the main reasons pertaining to auto insurance termination can help car owners prevent sacrificing one of the most critical privileges obtainable. Thanks for the strategies shared through your blog.

  9. Thanks for the good writeup. It if truth be told was a enjoyment account it. Look complicated to far brought agreeable from you! By the way, how could we keep in touch?

  10. Yrefgpw表示:

    Здравствуйте!
    Купить диплом университета.
    arzymat.blogspot.com/2016/06/blog-post

  11. Dnrtolo表示:

    Привет, друзья!
    Заказать документ о получении высшего образования вы имеете возможность в нашей компании в столице.
    vk.com/unikvseru?w=wall414329581_6380

  12. Cazrzwv表示:

    Привет!
    Мы готовы предложить документы техникумов
    f99538ch.bget.ru/main/1-post1

  13. Treftud表示:

    Добрый день!
    Купить диплом о среднем полном образовании, в чем подвох и как избежать обмана?
    ssd-astra.ru/forum/viewtopic.php?f=14&t=12709
    Окажем помощь!.

  14. Cazrdlo表示:

    Здравствуйте!
    Мы предлагаем документы техникумов
    polkasocial.org/read-blog/27296

  15. I?ll right away grab your rss as I can’t find your e-mail subscription link or newsletter service. Do you’ve any? Please let me know in order that I could subscribe. Thanks.

  16. Yrefgsk表示:

    Добрый день!
    Приобрести диплом университета.
    веб-дидактика.рф/2022/03/blog-post

  17. Dnrtvbs表示:

    Добрый день!
    Заказать документ института можно в нашей компании в столице.
    forum.storeland.ru/index.php?/user/34922-hnfmrfdtg/

  18. Mazrvwq表示:

    Добрый день!
    Приобретение школьного аттестата с официальным упрощенным обучением в Москве
    sabrina-online.su/wiki/index.php?title=luxdiplomru

  19. Sazrdjp表示:

    Добрый день!
    Диплом о высшем образовании
    telegra.ph/stoimost-obucheniya-v-vuzah-2018-08-02

  20. Sazrbxv表示:

    Привет!
    Заказать документ института
    enote-store.ru/?rz=a

  21. Treftup表示:

    Здравствуйте!
    Узнайте, как приобрести диплом о высшем образовании без рисков
    barlophotos.blogspot.com/2018/06/avto-marshrut-lagonaki
    Поможем вам всегда!.

  22. Dnrtcls表示:

    Добрый день!
    Приобрести документ о получении высшего образования вы имеете возможность в нашей компании в Москве.
    t.me/jevev/7852

  23. Hi there! I know this is kinda off topic however I’d figured I’d ask. Would you be interested in exchanging links or maybe guest writing a blog post or vice-versa? My site goes over a lot of the same subjects as yours and I believe we could greatly benefit from each other. If you happen to be interested feel free to send me an email. I look forward to hearing from you! Wonderful blog by the way!

  24. Cazriag表示:

    Добрый день!
    Купить диплом о высшем образовании
    telegra.ph/v-processe-podgotovki-k-postupleniyu-v-tehnicheskij-vuz-starsheklassnik-08-02

  25. Interesting post made here. One thing I’d like to say is the fact that most professional areas consider the Bachelor’s Degree like thejust like the entry level standard for an online course. Whilst Associate College diplomas are a great way to begin with, completing your current Bachelors opens up many good opportunities to various employment goodies, there are numerous on-line Bachelor Diploma Programs available through institutions like The University of Phoenix, Intercontinental University Online and Kaplan. Another issue is that many brick and mortar institutions offer Online versions of their diplomas but often for a significantly higher cost than the corporations that specialize in online college diploma plans.

  26. Cazriav表示:

    Здравствуйте!
    Мы готовы предложить документы ВУЗов
    jsfiddle.net/OnManuals/74t1pjo0/1

  27. Diplomi_irEa表示:

    Привет!
    Заказать документ института можно в нашем сервисе.
    автолнр.рф/users/idinuqoja

  28. Yrefnxz表示:

    Здравствуйте!
    Приобрести диплом о высшем образовании.
    aluformsarl.ch/2015/07/10/sed-posuere-consectetur-est-at-lobortis/#comment-76148

  29. Trefeym表示:

    Привет, друзья!
    Как получить диплом техникума с упрощенным обучением в Москве официально
    1001stenag.co.za/quisque-sit-amet-sapien-et-lacus/#comment-74865
    Окажем помощь!.

  30. WilliamDeeli表示:

    Привет, друзья!
    Мы предлагаем дипломы.
    telegra.ph/Predpochitaete-kupit-diplom-o-vysshem-obrazovanii-01-20

發佈留言

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