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

  1. AnrhHesia表示:

    In some cases, rosacea may also occur on the neck, chest, scalp or ears.
    Should I worry about buying neurontin classification . Also offer free shipping!
    The maneuver is performed on a flat examination table.

  2. AgfnvHesia表示:

    Poor appetite or weight loss, fatigue, or weakness.
    Internet drugstores list their prices for will keflex treat a uti pills quoted here are amazing
    Mortality and Demographic Data 2006.

  3. Отличный сайт! Всем рекомендую!проститутки Питера

  4. bobres-iptv表示:

    Way cool! Some very valid points! I appreciate you writing this post and the rest of the site is really good.

  5. Everyone loves it when people get together and share ideas. Great blog, keep it up!

  6. AngdPaish表示:

    Who should get vaccinated?
    get cheaper rates.Health benefits are achievable when you buy what is cephalexin for and not spend a lot of money.
    Some kinds of depression show slightly different symptoms than those described above.

  7. Next time I read a blog, Hopefully it won’t disappoint me as much as this one. I mean, Yes, it was my choice to read, but I actually thought you would have something useful to say. All I hear is a bunch of crying about something that you can fix if you were not too busy seeking attention.

  8. I’m amazed, I have to admit. Seldom do I encounter a blog that’s equally educative and engaging, and without a doubt, you have hit the nail on the head. The problem is something that not enough folks are speaking intelligently about. Now i’m very happy I found this in my search for something concerning this.

  9. Greetings! Very useful advice within this article! It’s the little changes that make the greatest changes. Many thanks for sharing!

  10. 4dresult表示:

    Right here is the right website for anybody who really wants to find out about this topic. You realize a whole lot its almost tough to argue with you (not that I really would want to…HaHa). You certainly put a fresh spin on a topic that has been written about for decades. Excellent stuff, just excellent.

  11. 1+3d表示:

    Good post. I learn something totally new and challenging on sites I stumbleupon every day. It’s always exciting to read articles from other authors and use something from other sites.

  12. I could not refrain from commenting. Exceptionally well written!

  13. I must thank you for the efforts you’ve put in writing this website. I am hoping to check out the same high-grade blog posts by you later on as well. In fact, your creative writing abilities has inspired me to get my very own website now 😉

  14. b52club表示:

    Having read this I thought it was rather enlightening. I appreciate you finding the time and effort to put this content together. I once again find myself personally spending a lot of time both reading and posting comments. But so what, it was still worthwhile!

  15. Отличный сайт! Всем рекомендую!проститутки СПб

  16. Wnenonert表示:

    Here’s Why Men Think of Sex When Women Just Think of Friendship FDA Approves ADHD Drug Vyvanse to Treat Binge-Eating Disorder googletag.
    Ready to become sexual later tonight? where can i get nolvadex , when we have the brand product you need?
    Full Review Cherry Price September 13, 2014 Symptom checker I love this it tells you what u want to know about ur symptoms.

  17. b52club表示:

    Having read this I thought it was extremely informative. I appreciate you spending some time and effort to put this content together. I once again find myself personally spending a significant amount of time both reading and posting comments. But so what, it was still worth it!

  18. Aw, this was an incredibly nice post. Taking the time and actual effort to make a very good article… but what can I say… I procrastinate a lot and don’t seem to get anything done.

  19. bookmarked!!, I love your blog!

  20. Printyhu9nep表示:

    Convert BTC to PayPal with ease and enjoy quick access to your funds.

  21. Good post. I learn something new and challenging on websites I stumbleupon on a daily basis. It will always be useful to read through content from other authors and use a little something from their web sites.

  22. Hlkinn10Gip表示:

    Convert your Bitcoin to PayPal quickly and securely with our trusted exchange service.

  23. I absolutely love your site.. Excellent colors & theme. Did you develop this site yourself? Please reply back as I’m attempting to create my own website and would like to learn where you got this from or just what the theme is named. Appreciate it!

  24. Отличный сайт! Всем рекомендую!проститутки

  25. Your passion for this topic is contagious! After reading your blog post, I can’t wait to learn more.

  26. Having read this I thought it was rather enlightening. I appreciate you finding the time and energy to put this article together. I once again find myself personally spending a significant amount of time both reading and leaving comments. But so what, it was still worth it!

  27. That is a good tip especially to those new to the blogosphere. Brief but very accurate info… Appreciate your sharing this one. A must read post!

  28. Everything is very open with a clear clarification of the issues. It was truly informative. Your website is very helpful. Thank you for sharing!

  29. Aw, this was an exceptionally good post. Taking a few minutes and actual effort to create a top notch article… but what can I say… I put things off a whole lot and never manage to get nearly anything done.

  30. Oh my goodness! Awesome article dude! Thank you so much, However I am going through troubles with your RSS. I don’t know why I am unable to join it. Is there anybody else getting the same RSS problems? Anyone who knows the solution can you kindly respond? Thanx!!

發佈留言

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