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

  1. Leonardpar表示:

    https://indiapharmacy.shop/# Online medicine home delivery

  2. DouglasGed表示:

    buying prescription drugs in mexico: Mexico pharmacy online – mexican rx online

  3. Diplomi_pkEa表示:

    Добрый день!
    Купить документ института вы можете у нас.
    active-motor-ru.1gb.ru/index.php?subaction=userinfo&user=oguven

  4. DouglasGed表示:

    india pharmacy mail order: Online pharmacy USA – top 10 online pharmacy in india

  5. Mazrsnw表示:

    Добрый день!
    Полезные советы по покупке диплома о высшем образовании без риска
    crazyhandmadic.blogspot.com/2013/05/1

  6. Leonardpar表示:

    http://mexicopharmacy.win/# buying from online mexican pharmacy

  7. Lazrrcc表示:

    Привет, друзья!
    Купить диплом университета.
    telegra.ph/tehnologii-obucheniya-v-vuze-08-02

  8. DouglasGed表示:

    top 10 pharmacies in india: Online medicine home delivery – best online pharmacy india

  9. Cazriyc表示:

    Привет, друзья!
    Мы предлагаем документы техникумов
    forums.indexrise.com/user-357710

  10. DouglasGed表示:

    mexican drugstore online: Medicines Mexico – pharmacies in mexico that ship to usa

  11. Leonardpar表示:

    https://edpillpharmacy.store/# best online ed meds

  12. Mazrgad表示:

    Привет!
    Официальная покупка диплома вуза с упрощенной программой обучения
    allalarkin.blogspot.com/2014/04/blog-post

  13. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several e-mails with the same comment. Is there any way you can remove me from that service? Bless you!

  14. Diplomi_udEa表示:

    Привет!
    Заказать документ университета можно в нашей компании в столице.
    rf-4fun.ru/index.php?/profile/1774-michellthymn

  15. Lazrdhi表示:

    Здравствуйте!
    Мы готовы предложить дипломы любой профессии по приятным ценам.
    telegra.ph/kakie-dokumenty-podavat-dlya-postupleniya-v-vuz-08-02

  16. Yrefmey表示:

    Здравствуйте!
    Заказать диплом о высшем образовании.
    newsinfa.ru/forums/topic/kuplyu-diplomy-kandidata-nauk-g759d

  17. Diplomi_hyEa表示:

    Привет, друзья!
    Заказать документ института можно в нашей компании в столице.
    shoovi.blogspot.com/2014/12/benefit-porefessional-pro-balm-to

  18. Thanks for another wonderful article. Where else could anybody get that kind of information in such an ideal way of writing? I have a presentation next week, and I’m on the look for such information.

  19. DouglasGed表示:

    mexico drug stores pharmacies: Medicines Mexico – mexico drug stores pharmacies

  20. Trefoht表示:

    Привет, друзья!
    Парадокс, но купить диплом кандидата наук оказалось не так и сложно
    onlineboxing.net/jforum/user/profile/299761.page
    Всегда вам поможем!.

  21. DouglasGed表示:

    mexican online pharmacies prescription drugs: Certified Mexican pharmacy – best online pharmacies in mexico

  22. Lazrkaa表示:

    [u][b] Здравствуйте![/b][/u]
    [b]Мы предлагаем дипломы[/b] любой профессии по приятным ценам.
    [url=http://telegra.ph/vtoroj-potok-postupleniya-v-vuz-08-02/]telegra.ph/vtoroj-potok-postupleniya-v-vuz-08-02[/url]

  23. Yreflrt表示:

    [u][b] Привет, друзья![/b][/u]
    [b]Купить диплом о высшем образовании.[/b]
    [url=http://notebooks.ru/forum/user/69033/]notebooks.ru/forum/user/69033[/url]

  24. I do believe all the ideas you have offered to your post. They are very convincing and can certainly work. Still, the posts are too brief for beginners. May you please extend them a little from subsequent time? Thanks for the post.

  25. Sazroni表示:

    [u][b] Добрый день![/b][/u]
    Заказать документ о получении высшего образования
    [url=http://nextvio.net/read-blog/584/]nextvio.net/read-blog/584[/url]
    [url=http://sonnick84.getblogs.net/60649444/Желаете-приобрести-аттестат-у-проверенного-производителя-Ждем-Вас/]sonnick84.getblogs.net/60649444/Желаете-приобрести-аттестат-у-проверенного-производителя-Ждем-Вас[/url]
    [url=http://mpett.com/blogs/692/Правильно-приобретаем-диплом-в-интернете-обзор/]mpett.com/blogs/692/Правильно-приобретаем-диплом-в-интернете-обзор[/url]
    [url=http://ecoheal.ru/index.php/jomsocial/groups/viewbulletin/292-dokumenty-po-vygodnym-rastsenkam-v-izvestnom-magazine?groupid=1/]ecoheal.ru/index.php/jomsocial/groups/viewbulletin/292-dokumenty-po-vygodnym-rastsenkam-v-izvestnom-magazine?groupid=1[/url]
    [url=http://sonnick84.blogars.com/27687316/Стоимость-документов-авторский-обзор-специалистов/]sonnick84.blogars.com/27687316/Стоимость-документов-авторский-обзор-специалистов[/url]

  26. Yrefpns表示:

    [u][b] Здравствуйте![/b][/u]
    [b]Купить диплом любого ВУЗа.[/b]
    [url=http://msfo-soft.ru/msfo/forum/messages/forum30/topic10518/message302224/?result=new#message302224/]msfo-soft.ru/msfo/forum/messages/forum30/topic10518/message302224/?result=new#message302224[/url]

  27. DouglasGed表示:

    erectile dysfunction online prescription: Best ED meds online – best ed medication online

  28. Diplomi_klEa表示:

    Добрый день!
    Заказать документ о получении высшего образования можно у нас.
    laemngophos.org/webboard/showthread.php?tid=78224

  29. Leonardpar表示:

    http://indiapharmacy.shop/# Online medicine home delivery

  30. Trefcus表示:

    Привет!
    Как приобрести диплом техникума с минимальными рисками
    led119.ru/forum/user/108242
    Будем рады вам помочь!.

發佈留言

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