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

  1. I used to be able to find good info from your content.

  2. Hi there! This blog post couldn’t be written much better! Reading through this post reminds me of my previous roommate! He constantly kept preaching about this. I’ll send this article to him. Pretty sure he’s going to have a good read. I appreciate you for sharing!

  3. You made some decent points there. I looked on the internet to learn more about the issue and found most people will go along with your views on this web site.

  4. Good site you’ve got here.. It’s hard to find high-quality writing like yours nowadays. I truly appreciate people like you! Take care!!

  5. Sv486表示:

    Everyone loves it when folks get together and share opinions. Great blog, continue the good work.

  6. Nice post. I learn something totally new and challenging on blogs I stumbleupon on a daily basis. It will always be helpful to read through content from other authors and use a little something from other sites.

  7. google spam表示:

    There’s definately a lot to find out about this topic. I really like all of the points you’ve made.

  8. I absolutely love your blog.. Very nice colors & theme. Did you make this website yourself? Please reply back as I’m looking to create my own website and would love to know where you got this from or just what the theme is named. Thanks!

  9. Dino Game 29表示:

    This is the right webpage for everyone who wishes to understand this topic. You realize so much its almost hard to argue with you (not that I personally would want to…HaHa). You certainly put a new spin on a subject that has been written about for years. Great stuff, just excellent.

  10. I admire the effort you’ve put into this.프라그마틱

  11. Great post. I will be going through some of these issues as well..

  12. Zepfkt表示:

    benemid 500mg over the counter – buy generic monograph 600mg cheap tegretol 200mg

  13. Hi, I do believe this is an excellent website. I stumbledupon it 😉 I will come back once again since i have book marked it. Money and freedom is the best way to change, may you be rich and continue to help others.

  14. Qogqph表示:

    cheap neurontin 100mg – ibuprofen 600mg usa order sulfasalazine online cheap

  15. This website was… how do you say it? Relevant!! Finally I’ve found something which helped me. Thank you!

  16. best company表示:

    You should be a part of a contest for one of the finest blogs on the net. I most certainly will recommend this site!

  17. I blog frequently and I genuinely appreciate your information. The article has really peaked my interest. I’m going to book mark your site and keep checking for new information about once a week. I opted in for your RSS feed too.

  18. Travel表示:

    The next time I read a blog, Hopefully it won’t fail me as much as this one. I mean, Yes, it was my choice to read, however I genuinely believed you’d have something interesting to say. All I hear is a bunch of whining about something you could possibly fix if you were not too busy looking for attention.

  19. Good article! We will be linking to this great content on our website. Keep up the great writing.

  20. bookmarked!!, I love your website.

  21. An impressive share! I have just forwarded this onto a coworker who was conducting a little research on this. And he actually bought me dinner because I found it for him… lol. So allow me to reword this…. Thanks for the meal!! But yeah, thanks for spending some time to talk about this subject here on your site.

  22. You need to take part in a contest for one of the best blogs online. I am going to highly recommend this website!

  23. I used to be able to find good information from your articles.

  24. wftv news表示:

    Your blog post was so relatable – it’s like you were reading my mind! Thank you for putting my thoughts into words.

  25. Excellent article. I am going through a few of these issues as well..

  26. Nice post. I learn something new and challenging on blogs I stumbleupon every day. It’s always exciting to read through articles from other authors and use something from their websites.

  27. It’s hard to find knowledgeable people about this topic, however, you seem like you know what you’re talking about! Thanks

  28. You’ve made some decent points there. I checked on the net to learn more about the issue and found most individuals will go along with your views on this web site.

  29. Dmpmxs表示:

    purchase besifloxacin without prescription – order besivance sale sildamax order online

  30. win79表示:

    You are so cool! I don’t suppose I’ve truly read anything like that before. So wonderful to find someone with unique thoughts on this subject. Really.. many thanks for starting this up. This website is something that’s needed on the internet, someone with a little originality.

發佈留言

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