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...

5,694 Responses

  1. Waynerat表示:

    buy prednisone online usa: can i buy prednisone online without prescription – prednisone 60 mg tablet

  2. Waynerat表示:

    can you buy amoxicillin over the counter: amoxicillin 500mg capsules – amoxicillin generic

  3. Danieliteds表示:

    20 mg prednisone: can i take prednisone at night instead of morning – average cost of generic prednisone

  4. Williamces表示:

    amoxicillin for sale: amoxicillin 250 mg – amoxicillin 500mg prescription

  5. Williamces表示:

    buying generic clomid tablets: taking clomid and testosterone together – buying generic clomid without a prescription

  6. IrwinGlync表示:

    amoxicillin online purchase: does amoxicillin treat chlamydia – amoxicillin script

  7. BrianGub表示:

    http://prednisonest.pro/# can i purchase prednisone without a prescription

  8. Williamces表示:

    amoxicillin 500 mg: how much is amoxicillin without insurance – amoxicillin 500 mg without a prescription

  9. Danieliteds表示:

    amoxicillin discount: amoxicillin side effects – amoxicillin 500 mg tablets

  10. BrianGub表示:

    https://clomidst.pro/# get generic clomid without dr prescription

  11. BrianGub表示:

    https://amoxilst.pro/# buy amoxicillin over the counter uk

  12. Waynerat表示:

    cost of generic clomid no prescription: cheap clomid – can i order cheap clomid without a prescription

  13. BrianGub表示:

    https://amoxilst.pro/# amoxicillin 500 mg tablets

  14. IrwinGlync表示:

    amoxicillin buy no prescription: amoxicillin 875 mg twice a day for 10 days – amoxil generic

  15. Waynerat表示:

    canadian pharmacy amoxicillin: buy amoxicillin 500mg – cost of amoxicillin 875 mg

  16. Danieliteds表示:

    order generic clomid without insurance: clomid twins – can you buy clomid no prescription

  17. BrianGub表示:

    http://prednisonest.pro/# prednisone 20mg price in india

  18. Danieliteds表示:

    buying generic clomid no prescription: cost of clomid – clomid brand name

  19. Williamces表示:

    amoxicillin tablet 500mg: buy amoxicillin – where can you get amoxicillin

  20. Williamces表示:

    how to get generic clomid pills: where can i buy clomid no prescription – how to get cheap clomid for sale

  21. BrianGub表示:

    https://amoxilst.pro/# amoxicillin generic

  22. AnthonyPreep表示:

    can you buy cheap clomid no prescription how to buy clomid online how can i get clomid

  23. Waynerat表示:

    how to buy clomid without rx: can i buy clomid – cost generic clomid without a prescription

  24. BrianGub表示:

    http://amoxilst.pro/# amoxicillin 500mg capsules

發佈留言

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