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

8,502 Responses

  1. FrancisSmeds表示:

    https://datingtopreview.online/# online dating best sites free

  2. Geraldhoxia表示:

    https://datingtopreview.online/# internet dating

  3. Eeeoth表示:

    glucophage for sale buy amlodipine 10mg without prescription norvasc 5mg drug

  4. I just couldn’t leave your website before suggesting that I really enjoyed the usual information an individual supply on your visitors? Is gonna be back often in order to investigate cross-check new posts

  5. eltorotips表示:

    Hi, I just hopped over to your web-site through StumbleUpon. Not somthing I might typically browse, but I liked your views none the less. Thanks for making something worthy of reading through.

  6. This is something that will need all of our combined efforts to address.

  7. Geraldhoxia表示:

    https://datingtopreview.com/# match dating site

  8. Travisnem表示:

    free chat and dating online dating websites best

  9. Nvkiud表示:

    cialis walmart order generic tadalafil generic tadalafil 20mg

  10. I Am Going To have to come back again when my course load lets up – however I am taking your Rss feed so i can go through your site offline. Thanks.

  11. satta king表示:

    I think this is among the so much vital info for me. And i’m happy reading your article. But wanna remark on few common issues, The site style is wonderful, the articles is really excellent : D. Just right job, cheers

  12. Uxehsk表示:

    order desyrel 50mg pill order trazodone for sale order viagra 150mg online cheap

  13. FrancisSmeds表示:

    https://datingtopreview.com/# 1 dating sites

  14. I like this weblog very much so much great info .

  15. Geraldhoxia表示:

    https://datingtopreview.online/# christian dating for free

  1. 2022-10-20

    gay daddies free chat https://gaymanchatrooms.com/

  2. 2022-10-20

    can someone write my paper https://term-paper-help.org/

  3. 2022-10-21

    custom paper writing service https://sociologypapershelp.com/

  4. 2022-10-21
  5. 2022-10-21

    help writing a college paper https://writepapersformoney.com/

  6. 2022-10-21

    write my college paper for me https://write-my-paper-for-me.org/

  7. 2022-10-21

    need someone write my paper https://doyourpapersonline.com/

  8. 2022-10-21

    pay someone to write a paper for me https://top100custompapernapkins.com/

  9. 2022-10-21

    purchase college papers https://researchpaperswriting.org/

  10. 2022-10-21

    college paper writers https://cheapcustompaper.org/

  11. 2022-10-21

    paper writing services reviews https://writingpaperservice.net/

  12. 2022-10-21

    buy a paper for college https://buyessaypaperz.com/

  13. 2022-10-21

    write my biology paper https://mypaperwritinghelp.com/

發佈留言

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