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

  1. LEANBIOME表示:

    This article is extremely informative. I truly valued reading it. The details is very well-organized and simple to follow.

  2. Ebbscv表示:

    purchase cyclosporine for sale – colchicine us gloperba pill

  3. Great site! I recommend it to everyone!Book private flights

  4. porn表示:

    Saved as a favorite, I love your website!

  5. After going over a few of the blog articles on your site, I truly appreciate your way of blogging. I added it to my bookmark site list and will be checking back soon. Please check out my web site as well and tell me what you think.

  6. Loved the details in this post. It’s extremely well-researched and full of useful information. Excellent work!

  7. Excellent entry. It’s extremely articulate and filled with beneficial details. Many thanks for providing this information.

  8. Great site! I recommend it to everyone!Private jet empty legs

  9. Great site! I recommend it to everyone!Private jet charter

  10. Great info. Lucky me I found your site by chance (stumbleupon). I’ve book marked it for later!

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

  12. I am commenting to let you know what a exceptional experience my wife’s child enjoyed viewing your webblog. She picked up many details, with the inclusion of what it is like to have an amazing giving mindset to make a myriad of people effortlessly have an understanding of a variety of specialized subject matter. You honestly inspired her. Thank you for offering the important, safe, edifying not to mention easy pointers regarding your subject to Evelyn. Thanks,

  13. shower leak表示:

    when i do get my retirement, i would really love to relax near the beach and enjoy a home on a tropical country,.

  14. webcam sex表示:

    An impressive share! I have just forwarded this onto a coworker who had been doing a little research on this. And he in fact ordered me lunch due to the fact that I discovered it for him… lol. So let me reword this…. Thank YOU for the meal!! But yeah, thanks for spending the time to discuss this issue here on your site.

  15. bokep jepang表示:

    You have made some really good points there. I checked on the net to find out more about the issue and found most individuals will go along with your views on this site.

  16. I’m impressed, I have to admit. Really rarely can i encounter a blog that’s both educative and entertaining, and without a doubt, you’ve hit the nail to the head. Your idea is outstanding; the problem is something which inadequate persons are speaking intelligently about. My business is very happy i found this in my look for some thing in regards to this.

  17. My brother recommended I might like this web site. He was totally right. This post actually made my day. You can not imagine simply how much time I had

  18. Whitescreen61表示:

    An interesting discussion is definitely worth comment. I do think that you need to write more on this topic, it may not be a taboo subject but generally folks don’t speak about these topics. To the next! Kind regards!

  19. leak tracing表示:

    it sometimes difficult to select the right kind of mens clothing but there are helpful buying guides on the internet.,

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

  21. I’m amazed, I have to admit. Seldom do I come across a blog that’s equally educative and engaging, and without a doubt, you’ve hit the nail on the head. The issue is an issue that too few people are speaking intelligently about. I am very happy I stumbled across this in my search for something relating to this.

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

  23. Thank you for another fantastic blog. Where else might one get that sort of knowledge written in such a perfect way? I actually have a presentation that i’m presently acting on, and i are yearning for such info instead.

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

  25. Aktafz表示:

    trileptal sale – buy pirfenex sale cheap synthroid tablets

  26. This in turn piece has a tendency to redeem considerable potential customers. How you would support it? Getting this done offers a cool uncommon perspective in issues. I suppose utilizing anything at all legitimate possibly major to convey home elevators is the most essential consideration.

  27. Very good info. Lucky me I recently found your blog by chance (stumbleupon). I’ve bookmarked it for later!

  28. There is apparently a lot to identify about this. I think you made some nice points in features also.

  29. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several e-mails with the same comment. Is there any way you can remove people from that service? Appreciate it!

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

發佈留言

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