AngularJS – Directive 介紹篇
接續前面提到的關於產品展示頁面的製作,我們在HTML文件上面加入一個Add to Cart的按鈕,但是希望這個按鈕可以有條件性的顯示或隱藏,我們可以在該按鈕的HTML標籤中加入ng-show=”store.product.canPurchase”的屬性,這個屬性會去判斷在gem數列中canPurchase中的值是否為true,若不是則該按鈕不顯示於畫面上,反之則會顯示加入購物車(Add to Cart)的按鈕。
在這個案例中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,也就是會隱藏所有的商品資訊。
不過這樣的表達方式並不是很直接,必須要做邏輯上的轉換,因為條件式是soldOut不等於true就顯示該商品,不是很容易搞懂,所以同樣的案例我們可以把HTML中的標籤的屬性改成ng-hide=”store.product.soldOut”,這樣一來我們只需要去看soldOut參數中的值是true或是false,如果賣完就不顯示;沒賣完就顯示,這是一個比較直覺的方法,透過JS Bin觀賞本案例的原始碼。
接下來看多項商品的製作方式,首先必須了解如何在Javascript的數列中加入多項商品的資料。
輸入了以上數列的資料後,在HTML的頁面上可以依下圖的方式呈現出多重商品的資訊,不過從以下的呈現方式可以看的出來,如果商品數量不僅僅是只有兩項,那頁面上面的HTML標籤就會過於複雜,所以下面的方法僅僅是讓大家了解邏輯的部分。
實際上呈現的方式可以參考下圖,是不是簡單多了,在JS Bin上面觀看。
接下來幫大家整理目前講解到的一些AngularJS的專有名稱說明:
- Directives(指示) – 在HTML中觸發Javascript行為的參數。
- Modules(模組) – 定義我們的應用程式。
- Controllers(控制器) – 定義我們應用程式該如運作。
- Expressions(表達式) – 我們的資料該如何的顯示在頁面上。
※以上截圖部分為AngularJS官方教學視訊,另有directive指令介紹的專頁。
I’m amazed, I have to admit. Seldom do I encounter a blog that’s both educative and interesting, and without a doubt, you have hit the nail on the head. The issue is something too few folks are speaking intelligently about. I’m very happy that I stumbled across this during my search for something concerning this.
Spot on with this write-up, I actually believe this web site needs a great deal more attention. I’ll probably be returning to see more, thanks for the information.
Great site! I recommend it to everyone!anal training kit
almost everyone in our family have hypertension, this disease is very common among older people“
This is how to get your foot in the door.
Your blog post was like a breath of fresh air. Thank you for reminding me to slow down and appreciate the beauty of life.
Great site! I recommend it to everyone!big dildos
My brother recommended I might like this website. He was entirely right. This post truly made my day. You cann’t imagine just how much time I had spent for this info! Thanks!
After the chemotherapy is completed, the stored stem cells are thawed and given back to the patient through an infusion.
If you need to save money on does sildenafil work as good as viagra with free delivery.
Typhimurium, public health laboratories may perform bacteriophage typing or pulsed-field gel electrophoresis PFGE to further characterize the drug-resistance patterns of these organisms.
I’ve been interested in this info and ultimately found this blog. I’d personally wish to be capable of getting my hands on the information that you tapped to expose this. Please continue on your quest.
This is a great blog and i want to visit this every day of the week “
Adored the information shared in this article. It’s very well-written and full of valuable details. Excellent effort!
I am glad to be one of many visitants on this outstanding site (:, regards for posting .
Thanks for the auspicious writeup. It if truth be told was once a entertainment account it. Glance complex to far delivered agreeable from you! By the way, how could we keep in touch?
Further investigation is required.
Look no further for the most effective treatment and sildenafil citrate price . See how it works!
Chris Hi Midge Yes, most definitely you can have these symptoms without it being due to HIV.
I like what you guys are up too. Such intelligent work and reporting! Carry on the excellent works guys I have incorporated you guys to my blogroll. I think it will improve the value of my website 🙂
I’ve discovered at this point a good deal helpful data. I can endorse this page.
Fantastic post. I discovered the content very useful. Adored the method you detailed the content.
There is definately a lot to find out about this issue. I like all the points you’ve made.
Отличный сайт! Всем рекомендую!запчасти на иномарки
Good, i’ve got to assert that the producing skills not which unhealthy, yet i can also you can actually improve ones own publishing knowledge. Implementing reduced thoughts to mention the actual. You’ll check out search engine just for making skills schooling website and applications.
This website doesn’t show up properly on my i phone – you might wanna try and repair that
Greetings! Very helpful advice within this post! It is the little changes which will make the greatest changes. Thanks a lot for sharing!
I used to be able to find good information from your blog posts.
Wow, amazing blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your website is great, as well as the content!
After looking over a number of the articles on your website, I really appreciate your technique of blogging. I saved as a favorite it to my bookmark website list and will be checking back soon. Take a look at my web site as well and tell me what you think.
cheap rumalaya online – order rumalaya sale elavil 50mg pills
Great job, wonderful blog… really enjoy it and put into my social bookmarks. Keep up the good work
Oh my goodness! Amazing article dude! Thank you, However I am encountering troubles with your RSS. I don’t understand why I cannot subscribe to it. Is there anybody else having the same RSS problems? Anyone who knows the answer can you kindly respond? Thanx!!
Greetings! Very helpful advice in this particular article! It is the little changes which will make the most important changes. Many thanks for sharing!