AngularJS – Directive 介紹篇

接續前面提到的關於產品展示頁面的製作,我們在HTML文件上面加入一個Add to Cart的按鈕,但是希望這個按鈕可以有條件性的顯示或隱藏,我們可以在該按鈕的HTML標籤中加入ng-show=”store.product.canPurchase”的屬性,這個屬性會去判斷在gem數列中canPurchase中的值是否為true,若不是則該按鈕不顯示於畫面上,反之則會顯示加入購物車(Add to Cart)的按鈕。

AngularJS Part3 Slide1

在這個案例中canPurchase就算是在數列中的顯示參數,其實這個參數並不是一定要叫canPurchase這個名稱,可以自行決定名稱,例如在馬老師製作的案例中,我就是利用buttonShow這樣的參數名稱,大家也可以在JS Bin上面看看這個案例的程式碼。

<!DOCTYPE html>
<html ng-app="store">
<head>
<meta name="description" content="AngularJS directivesExample 1">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
  <meta charset="utf-8">
  <title>AngularJS directivesExample 1</title>
</head>
<body>
  
  <div ng-controller="StoreController as store">
    <h1>{{ store.product.name }}</h1>
    <h2>NT${{ store.product.price }}</h2>
    <p>{{ store.product.description }}</p>
    <button ng-show="store.product.buttonShow">加入購物車</button>
  </div>
  
</body>
</html>
(function(){
  var app = angular.module('store',[]);
  
  app.controller('StoreController',function(){
    this.product = item;
  });
  
  var item = {
    name:"馬老師雲端研究室 滑鼠墊",
    price:"350",
    description:"有些人認為滑鼠墊就只是種簡單的產品。無非是將滑鼠放於上方並將之滑來拖去。 “表面”上來說確實如此(一語雙關)。但實際上,製造能帶來卓越遊戲體驗的滑鼠墊卻需要結合眾多科學、技術和工藝。身為遊戲滑鼠技術的世界領先者,對如何使滑鼠感測器高效運作我們獨樹一幟。運用專門知識來製造適合的滑鼠墊表面,讓滑鼠感測器得以作出高效而精確的反應。",
    buttonShow: false
  };
  
})();

另外還有一種資料顯示的手法,我們可以在gem數列中另外加入soldOut這個參數,而在div的HTML標籤中加入ng-show=”!store.product.soldOut”這樣的屬性,其中驚嘆號指的是不等於,所以這個屬性的意思即是當soldOut參數不等於true的時候要隱藏該div,也就是會隱藏所有的商品資訊。

AngularJS Part3 Slide2

不過這樣的表達方式並不是很直接,必須要做邏輯上的轉換,因為條件式是soldOut不等於true就顯示該商品,不是很容易搞懂,所以同樣的案例我們可以把HTML中的標籤的屬性改成ng-hide=”store.product.soldOut”,這樣一來我們只需要去看soldOut參數中的值是true或是false,如果賣完就不顯示;沒賣完就顯示,這是一個比較直覺的方法,透過JS Bin觀賞本案例的原始碼。

AngularJS Part3 Slide3

接下來看多項商品的製作方式,首先必須了解如何在Javascript的數列中加入多項商品的資料。

AngularJS Part3 Slide4

輸入了以上數列的資料後,在HTML的頁面上可以依下圖的方式呈現出多重商品的資訊,不過從以下的呈現方式可以看的出來,如果商品數量不僅僅是只有兩項,那頁面上面的HTML標籤就會過於複雜,所以下面的方法僅僅是讓大家了解邏輯的部分。

AngularJS Part3 Slide5

實際上呈現的方式可以參考下圖,是不是簡單多了,在JS Bin上面觀看。

AngularJS Part3 Slide6

接下來幫大家整理目前講解到的一些AngularJS的專有名稱說明:

  • Directives(指示) – 在HTML中觸發Javascript行為的參數。
  • Modules(模組) – 定義我們的應用程式。
  • Controllers(控制器) – 定義我們應用程式該如運作。
  • Expressions(表達式) – 我們的資料該如何的顯示在頁面上。
AngularJS Part3 Slide7

※以上截圖部分為AngularJS官方教學視訊,另有directive指令介紹的專頁

You may also like...

3,198 Responses

  1. Timothystoft表示:

    online shopping pharmacy india: india online pharmacy – indian pharmacy paypal

  2. My brother recommended I might like this web site. He was entirely right. This post truly made my day. You can not imagine simply how much time I had spent for this information! Thanks!

  3. Timothystoft表示:

    buying prescription drugs in mexico: best online pharmacies in mexico – mexican pharmaceuticals online

  4. Erjbzz表示:

    omnicef 300 mg drug – purchase cefdinir without prescription clindamycin for sale

  5. JosephFam表示:

    prednisone 500 mg tablet: generic prednisone cost – where can i order prednisone 20mg

  6. JosephFam表示:

    prednisone cost canada: prednisone online pharmacy – where can i buy prednisone without a prescription

  7. JosephFam表示:

    where can i buy ventolin online: ventolin tablets – ventolin with out prescription

  8. Jrhrlk表示:

    cheap accutane 10mg – order deltasone 20mg generic deltasone 20mg us

  9. ArnvPaish表示:

    Some people with mild anemia, particularly when it develops slowly, have no symptoms at all.
    If prices for tadalafil for working out for quick resolutions on erection problems.
    Serious complications include dengue haemorrhagic fever or dengue shock syndrome.

  10. ArnfPaish表示:

    Sorry, you already requested your free pin.
    Some Internet pharmacies are reputable places to chrisale tadalafil with wholesale discounts
    My parents, too, both of whom have been on antidepressants for nearly 2 decades, are extremely volatile.

  11. JosephFam表示:

    semaglutide: Semaglutide pharmacy price – rybelsus price

  12. JosephFam表示:

    how much is ventolin in canada: buy albuterol inhaler – ventolin for sale canada

  13. JosephFam表示:

    prednisone for cheap: buy prednisone canadian pharmacy – prednisone 20mg prescription cost

  14. Explore the benefits of decentralized asset swapping with the innovative and user-friendly platform provided by anyswap exchange

  15. ArnoldDRODA表示:

    buy furosemide online: cheap lasix – buy lasix online

  16. AtndHesia表示:

    This condition is called white coat hypertension.
    is not such a good ideaPeople look for the cheapest price of can you take levitra and viagra together on account it is modestly-priced and produces exceptional
    Continuous, ongoing therapy groups have helped hundreds of people overcome social anxiety since 1994.

  17. AvctbHesia表示:

    BTW, if the pain gets worse, see a doctor, even if you get a negative.
    Get brand medicines at discount prices when you bestsellers about your problem.
    When should I call my healthcare provider about my wheezing?

  18. Appreciated this entry. It’s extremely well-researched and filled with valuable insights. Great work!

  19. ArnoldDRODA表示:

    furosemide 40mg: furosemide online – lasix dosage

  20. ArnoldDRODA表示:

    semaglutide: buy semaglutide online – buy semaglutide online

  21. ArnoldDRODA表示:

    prednisone 30 mg daily: buy prednisone with paypal canada – prednisone tablets

  22. AlqaPaish表示:

    Mental symptoms of Wilson’s diseaseWilson’s Disease affects approximately 1 in 30,000 people worldwide, making it a rare disorder.
    prices are available from pharmacies online for when you sublingual sildenafil will also be low.
    My partner says he has a burning feeling while urinating.

  23. AmmyPaish表示:

    Death related to pregnancy occurs approximately eight times in every 100,000 pregnancies.
    A proven way to sildenafil use from online pharmacies
    Irritation occurs not on-ly with manmade volatile substances, but also with naturally occurring substances such as perfumes, flowers, or onions.

  24. Your blog post was like a crash course in [topic]. I feel like I learned more in five minutes than I have in months of studying.

  25. plumber cost表示:

    I’d should talk with you here. Which is not some thing I usually do! I like reading an article which will make people believe. Also, thanks for allowing me to comment!

  26. Aw, this was a very good post. Taking the time and actual effort to make a really good article… but what can I say… I put things off a whole lot and never seem to get nearly anything done.

  27. PatrickLef表示:

    Farmacia online piГ№ conveniente: Farmacia online piu conveniente – comprare farmaci online all’estero

  28. PatrickLef表示:

    Farmacia online piГ№ conveniente: Tadalafil generico migliore – farmacie online affidabili

  29. PatrickLef表示:

    Farmacie on line spedizione gratuita: Ibuprofene 600 generico prezzo – farmacia online piГ№ conveniente

  30. PatrickLef表示:

    comprare farmaci online all’estero: Cialis generico controindicazioni – farmacie online sicure

發佈留言

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