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指令介紹的專頁。
You are so interesting! I do not think I have read through something like that before. So good to discover somebody with some unique thoughts on this issue. Really.. thanks for starting this up. This site is one thing that’s needed on the internet, someone with a bit of originality.
Some truly excellent information, Sword lily I detected this.
I love reading through an article that will make men and women think. Also, thanks for allowing me to comment.
online computers stores that offers good technical support are the best..
bookmarked!!, I really like your site!
You are so awesome! I do not think I’ve truly read something like this before. So great to find somebody with original thoughts on this subject matter. Really.. thank you for starting this up. This web site is something that is required on the web, someone with a little originality.
Отличный сайт! Всем рекомендую!автозапчасти в Минске
Hello there, There’s no doubt that your site may be having browser compatibility issues. When I take a look at your web site in Safari, it looks fine however, when opening in Internet Explorer, it’s got some overlapping issues. I simply wanted to give you a quick heads up! Apart from that, wonderful site!
Aw, this was a very good post. Taking a few minutes and actual effort to make a top notch article… but what can I say… I hesitate a lot and never seem to get nearly anything done.
hi!,I like your writing so much! share we communicate more about your article on AOL? I require an expert on this area to solve my problem. Maybe that’s you! Looking forward to see you.
Good post. I am dealing with some of these issues as well..
diclofenac 100mg uk – oral cambia buy aspirin 75mg online cheap
I tried to post something earlier today and I got a blank screen after I clicked ‘Comment’. It seems to work ok now I guess, but just so you know. Take care!
1хбет официальный сайт: 1xbet зеркало рабочее на сегодня – 1xbet зеркало рабочее на сегодня
1xbet скачать: 1xbet официальный сайт мобильная версия – зеркало 1хбет
1xbet зеркало: 1xbet официальный сайт – 1хбет официальный сайт
The majority of us don’t know how to start. Making it basic, you might be capability to ask all people nearest thing to you personally is most likely the best method to look for these types of condos in simply the subject associated with time and place.
Hello there! This post couldn’t be written much better! Looking at this article reminds me of my previous roommate! He constantly kept talking about this. I’ll send this post to him. Fairly certain he’ll have a very good read. Thank you for sharing!
Hi Today on google as well as loved reading through it greatly. I have bookmarked your site and you will be back again.
Howdy! This post couldn’t be written any better! Looking through this article reminds me of my previous roommate! He always kept talking about this. I’ll send this post to him. Fairly certain he’ll have a great read. I appreciate you for sharing!
An impressive share, I just with all this onto a colleague who was conducting a little analysis with this. And he in fact bought me breakfast because I uncovered it for him.. smile. So let me reword that: Thnx with the treat! But yeah Thnkx for spending time to go over this, I am strongly over it and really like reading more about this topic. When possible, as you grow expertise, does one mind updating your site with an increase of details? It is highly helpful for me. Big thumb up for this short article!
After exploring a handful of the articles on your web site, I seriously appreciate your technique of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Take a look at my website too and tell me your opinion.
Really appreciated this entry. It provided a lot of useful information. Excellent effort on composing this.
Your passion for this subject really shines through in your writing. It’s inspiring to see someone so enthusiastic about what they do.
Whats up are using WordPress for your blog platform? I’m new to the blog world but I’m trying to get started and set up my own. Do you need any html coding knowledge to make your own blog? Any help would be greatly appreciated!
It’s perfect time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I want to suggest you few interesting things or advice. Perhaps you could write next articles referring to this article. I desire to read more things about it!
Hello there! I just wish to give you a huge thumbs up for the excellent info you have got right here on this post. I’ll be returning to your site for more soon.
Excellent entry. I appreciate the work you put in to share such useful information. It is clear and extremely enlightening.
Oh my goodness! Incdredible article dude! Than yyou sso much,
Hwever I am experiencing difficulties with yor RSS. I don’t know thhe reason whyy I
cannott subszcribe tto it. Is there anone ele getting tthe same RSS issues?
Anyone that knnows thee answerr will you kindly respond?
Thanx!!
When I originally commented I appear to have clicked on the -Notify me when new comments are added- checkbox and from now on each time a comment is added I recieve 4 emails with the same comment. Is there an easy method you can remove me from that service? Thanks a lot.