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

22,350 Responses

  1. BradleyEdino表示:

    az parayla cok kazandiran slot oyunlar? az parayla cok kazandiran slot oyunlar? slot oyunlar?

  2. JasonUnsoG表示:

    https://sweetbonanza25.com/# sweet bonanza slot

  3. FrankEmine表示:

    slot oyunlar?: slot casino siteleri – en cok kazand?ran slot oyunlar?

  4. JasonUnsoG表示:

    http://denemebonusuverensiteler25.com/# yat?r?ms?z deneme bonusu veren siteler

  5. Wallacefex表示:

    guvenilir casino siteleri: guvenilir casino siteleri – canl? casino siteleri

  6. OLaneevige表示:

    Please let me know if you’re looking for a author for your weblog. You have some really great posts and I feel I would be a good asset. If you ever want to take some of the load off, I’d really like to write some content for your blog in exchange for a link back to mine. Please blast me an email if interested. Kudos!
    https://millionigrushek.ru/

  7. OLaneevige表示:

    Today, I went to the beach with my kids. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is entirely off topic but I had to tell someone!
    https://millionigrushek.ru/

  8. JasonUnsoG表示:

    https://sweetbonanza25.com/# sweet bonanza slot

  9. BradleyEdino表示:

    en guvenilir casino siteleri bahis siteleei bonus veren siteler yeni

  10. BradleyEdino表示:

    guvenilir slot siteleri slot casino siteleri guvenilir slot siteleri

  11. BradleyEdino表示:

    guvenilir casino siteleri Casino Siteleri Casino Siteleri

  12. JasonUnsoG表示:

    https://denemebonusuverensiteler25.com/# yeni deneme bonusu veren siteler

  13. Wallacefex表示:

    slot siteleri: slot siteleri – guvenilir slot siteleri

  14. FrankEmine表示:

    sweet bonanza giris: sweet bonanza giris – sweet bonanza kazanma saatleri

  15. Wallacefex表示:

    denemebonusuverensiteler25: deneme bonusu veren yeni siteler – deneme bonusu veren yeni siteler

  16. BradleyEdino表示:

    yat?r?ms?z deneme bonusu veren siteler deneme bonusu veren siteler deneme bonusu veren siteler yeni

  17. Wallacefex表示:

    sweet bonanza slot: sweet bonanza giris – sweet bonanza demo oyna

  18. FrankEmine表示:

    sweet bonanza slot: sweet bonanza demo oyna – sweet bonanza giris

  19. JasonUnsoG表示:

    https://slotsiteleri25.com/# slot casino siteleri

  20. JasonUnsoG表示:

    https://slotsiteleri25.com/# slot casino siteleri

  21. JasonUnsoG表示:

    https://denemebonusuverensiteler25.com/# deneme bonusu veren siteler yeni

  22. Wallacefex表示:

    sweet bonanza yorumlar: sweet bonanza demo oyna – sweet bonanza demo oyna

  23. BradleyEdino表示:

    slot oyunlar? guvenilir slot siteleri guvenilir slot siteleri

發佈留言

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