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 need to to thank you for this good read!! I absolutely loved every little bit of it. I have you saved as a favorite to check out new stuff you post…

  2. 匿名訪客表示:

    So an wonderful article! The insight offered is very useful and well-written. Many thanks for taking the time to compose this.

  3. Very good info. Lucky me I found your blog by accident (stumbleupon). I have book marked it for later!

  4. Yhmaqj表示:

    celecoxib 200mg sale – buy urispas pill order indomethacin pills

  5. When I originally commented I appear to have clicked on the -Notify me when new comments are added- checkbox and from now on every time a comment is added I receive four emails with the exact same comment. There has to be a way you are able to remove me from that service? Many thanks.

  6. This article is wonderful! Full of helpful information and very well-written. Thanks for sharing this.

  7. Greetings! Very helpful advice in this particular post! It is the little changes which will make the biggest changes. Thanks a lot for sharing!

  8. An outstanding share! I have just forwarded this onto a coworker who had been conducting a little homework on this. And he in fact ordered me dinner due to the fact that I stumbled upon it for him… lol. So allow me to reword this…. Thanks for the meal!! But yeah, thanx for spending time to talk about this subject here on your website.

  9. The next time I read a blog, I hope that it doesn’t disappoint me as much as this particular one. I mean, Yes, it was my choice to read, but I really thought you would have something interesting to say. All I hear is a bunch of whining about something that you can fix if you weren’t too busy seeking attention.

  10. Wonderful post! We are linking to this particularly great content on our site. Keep up the great writing.

  11. Oh my goodness! Awesome article dude! Thank you so much, However I am going through troubles with your RSS. I don’t understand why I can’t join it. Is there anybody else having the same RSS issues? Anyone who knows the solution will you kindly respond? Thanx!

  12. AngkHesia表示:

    Mold requires a compatible temperature for each species.
    Popular methods to buy can men take flagyl by shopping online.
    It deserves treatment just as much as any other condition.

  13. AgnvHesia表示:

    Please PLEASE go totally gluten free for just two weeks..
    Deals are available to is valtrex bad for covid-19 from.
    Job Classification In most duration tables, five job classifications are displayed.

  14. Vvdkdo表示:

    mebeverine brand – pletal over the counter pletal 100 mg drug

  15. Hey – good weblog, simply wanting around some blogs, seems a reasonably nice platform You Are using. I’m currently using Drupal for a couple of of my websites however seeking to change one among them over to a platform very a lot the identical to yours as a trial run. Anything particularly you’d advocate about it?

  16. I couldn’t resist commenting. Well written.

  17. AnedPaish表示:

    But my guess is that the president won’t be making that connection publicly any time soon.
    Check out how much you can save on does doxycycline treat uti illegally.
    HPV can damage the cells that line the cervix.

  18. This is a topic that is near to my heart… Take care! Where can I find the contact details for questions?

  19. 33win表示:

    I really like reading an article that can make people think. Also, thanks for allowing for me to comment.

  20. Wnwfonert表示:

    See the resources section below for more information on all aspects of HSV, including relationships.
    Immediately identify low prices and how long after taking lisinopril can i drink alcohol at cheap prices
    The material on this website is provided for educational purposes only and is not to be used for medical advise, diagnosis or treatment, or in a place of therapy or medical care.

  21. kontol表示:

    I used to be able to find good advice from your blog articles.

  22. kontol表示:

    Howdy, I do think your website could be having web browser compatibility issues. Whenever I take a look at your website in Safari, it looks fine but when opening in IE, it’s got some overlapping issues. I just wanted to give you a quick heads up! Apart from that, wonderful blog.

  23. ngentot pepek表示:

    This website was… how do you say it? Relevant!! Finally I have found something that helped me. Appreciate it!

  24. TK88表示:

    This is the right webpage for anyone who wishes to understand this topic. You understand a whole lot its almost tough to argue with you (not that I actually would want to…HaHa). You certainly put a brand new spin on a topic which has been discussed for a long time. Excellent stuff, just excellent.

  25. Good post! We will be linking to this particularly great content on our site. Keep up the good writing.

  26. Everything is very open with a precise clarification of the issues. It was truly informative. Your site is extremely helpful. Thanks for sharing!

  27. Your blog post was so thought-provoking. It’s rare to find content that challenges me to think deeply about important issues.

  28. I used to be able to find good info from your articles.

  29. Simplify your projects and boost productivity with easy-to-use project management software.

  30. I love it when people come together and share views. Great website, continue the good work!

發佈留言

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