AngularJS – Controller 介紹篇
繼續AngularJS的介紹,接下來我們看看controller的應用,以商品介紹的頁面為例,來看看這些資料要如何呈現在頁面上,想要呈現的資料有產品名稱、價格以及描述。
為了方便文章閱讀,本篇將application翻譯成應用程式;controller翻譯成控制器
控制器負責定義函數和值來決定整個應用程式的行為,同樣我們把Javascript和HTML分開來開發,在app.js內定義應用程式名稱和控制器的名稱、函數與值,在這次的案例中也提到利用(function(){})()包裝整個javascript是一種好習慣,有關於函式的基本觀念,大家可以閱讀一下這篇文章,介紹的還蠻詳細的!包裝部分的概念可參考其中介紹的立即函示(IIFE – Immediately Invoked Function Expression)。
定義好之後可以開始定義該控制器內的值,其中this.product=gem的意思是指未來在輸出時可以利用product.name去取得Dodecahedron;product.price去取得2.95…等等。
接下來就是把這些值顯示在網頁上,首先可以在div標籤內加上ng-controller=”StoreController as store”的屬性,其中ng-controller是去指定StoreController為該div內的控制器名稱;store是別名(類似綽號)未來呼叫store時就是在呼叫StoreController這個控制器。
在HTML標籤內開始輸出該控制器內的值,分別是:store.product.name、store.product.price、store.product.description三個項目,而在頁面上就會看到這三個值被顯示出來了。
需要注意的是,如果我們把輸出值部分的程式碼放在div外側的話,會沒有辦法順利的顯示值,因為我們的控制器是指定於div標籤內,離開了div標籤就無法取得該控制器了。
本篇的教學就到這邊,大家也可以透過JS Bin來測試或閱讀本次案例的程式碼。
<!DOCTYPE html> <html ng-app="store"> <head> <meta name="description" content="controllerExample"> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script> <meta charset="utf-8"> <title>controllerExample</title> </head> <body> <div ng-controller="StoreController as store"> <h1>{{ store.product.name }}</h1> <h2>{{ store.product.price }}</h2> <p>{{ store.product.description }}</p> </div> </body> </html> (function(){ var app = angular.module('store',[]); app.controller('StoreController',function(){ this.product = item; }); var item = { name:"馬老師雲端研究室 滑鼠墊", price:"350", description:"有些人認為滑鼠墊就只是種簡單的產品。無非是將滑鼠放於上方並將之滑來拖去。 “表面”上來說確實如此(一語雙關)。但實際上,製造能帶來卓越遊戲體驗的滑鼠墊卻需要結合眾多科學、技術和工藝。身為遊戲滑鼠技術的世界領先者,對如何使滑鼠感測器高效運作我們獨樹一幟。運用專門知識來製造適合的滑鼠墊表面,讓滑鼠感測器得以作出高效而精確的反應。", }; })();
※以上截圖部分為AngularJS官方教學視訊,另有控制器指令介紹的專頁。
17 Signs To Know If You Work With Foldaway Treadmill With Incline Portable
treadmill Incline, gigatree.eu,
Guide To Coffee Beans Uk: The Intermediate Guide To Coffee Beans Uk Coffee Beans Uk (https://Labo.Wodkcity.Com/Index.Php?Action=Profile;U=58946)
I unearthed your blog post to be a compelling and wise examination of the contemporary state of the
realm . Your analysis of the principal shifts and
hurdles confronting corporations in this realm was strikingly
powerful .
As an zealous proponent of this subject , I would be pleased to continue this conversation more thoroughly .
If you are inclined , I would enthusiastically implore you
to delve into the exciting options accessible at WM CASINO.
Our framework delivers a sophisticated and secure landscape for collaborating with aligned enthusiasts and securing
a wealth of knowledge to elevate your understanding of this dynamic field .
I eagerly await the chance of cooperating with you in the approaching time
Here is my web page … click here
How Treating Adults With ADHD Is A Secret Life Secret Life Of Treating
Adults With ADHD signs of untreated adhd (spectr-Sb116.ru)
How Best Budget Coffee Machine Has Changed My Life The Better Best Pod Coffee Machines Uk
20 Insightful Quotes About ADHD Treatment For Adults adhd treatment for adult women
20 Fun Informational Facts About Truck Accidents Attorneys truck accident Attorney
The Best Electric Treadmill Machine That Gurus Use 3 Things motorized electric treadmill
Many Of The Common Errors People Make With Injury Legal Injury attorneys
Asbestos was a sought-after building material
for a number of decades, but it is now well-known that exposure to Asbestos attorney can lead to serious and life-threatening diseases.
5 Laws Anybody Working In Birth Injury Law Should Be Aware Of Birth injury law firms
14 Businesses Doing A Great Job At Upvc Replacement Window Handles
Upvc Casement Window
Five People You Need To Know In The Private Adult Adhd Assessment Industry adult adhd assessment scotland
10 Things Everybody Hates About Veterans Disability
Attorneys veterans disability lawyer – http://led-5i8L419h33n.net/bbs/board.php?bo_table=0408&wr_id=20804,
Birth Injury Attorneys: 11 Thing That You’re Failing To Do
Birth injury law firms
hytrin 5mg tablet – flomax for sale online priligy 60mg cost
This Is The Retro Fridge Freezer 50 50 Case Study You’ll Never Forget modern side by side fridge
freezer – Kala,
The 10 Scariest Things About Repair Glass Repair Glass
Федеральный закон https://zakonobosago.ru “Об обязательном страховании гражданской ответственности владельцев транспортных средств” (ОСАГО)
5 Common Myths About Double Sided Dildoes You Should Stay Clear Of large double dildo (annunciogratis.Net)
What Is Online Shopping For Products And How To Utilize It?
top 10 online shopping sites in uk; Kristopher,
The Most Hilarious Complaints We’ve Been Hearing About Double Glazed Windows Near Me doubled glazed windows
10 Tell-Tale Symptoms You Need To Find A New Adhd In Women Symptoms list of Adhd symptoms
15 Presents For Your Porsche Replacement Key Lover
In Your Life How To Get a new porsche key
See What Replacement Vauxhall Key Tricks The Celebs Are Utilizing
vauxhall Key (telegra.ph)
This Is A Guide To ADHD In Adult Women In 2023 adhd symptoms for Women
The 10 Most Terrifying Things About Best Quality Childrens Bunk
Beds childrens bunk bed (castaneda-lott.Blogbright.net)
The Time Has Come To Expand Your Top Rated Mesothelioma Lawyers
Options alabama mesothelioma lawyers (Telegra.ph)
The 10 Most Scariest Things About Single Mattresses For Sale single mattresses For Sale
Why You Should Focus On Improving Double Glazed Window Replacement Window Double glazing replacement