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官方教學視訊,另有控制器指令介紹的專頁。
10 Key Factors About Test ADHD In Adults You Didn’t Learn In The Classroom online Test for adhd
14 Misconceptions Common To Accident Lawyers Baton Rouge Attorney Injury Accident Attorney
15 Gifts For The ADHD Assessment Private Lover In Your Life private adhd assessment milton keynes
A Sage Piece Of Advice On Adhd Assessment Adults From An Older
Five-Year-Old adult adhd assessment Scotland
A Glimpse Into The Secrets Of Nissan Key Programming how to Get A
new nissan key – sp001g.Dfix.Co.kr –
железобетонные изделия каталог железобетонные изделия каталог .
How To Tell The Upvc Window Doctor Near Me Which Is Right For You skylight window repair
Five Killer Quora Answers To ADHD Symptoms In Adult Women Adhd Symptoms In Adult Women
Volkswagen Polo Key Price Tools To Improve Your Daily
Lifethe One Volkswagen Polo Key Price Trick That Every Person Should Be Able To Volkswagen polo Key price
You’ll Be Unable To Guess Illinois Birth Injury
Attorneys’s Benefits illinois birth injury Attorneys
Greetings, associate enthusiast. I must extol you on your keen analysis of the subject matter presented in this blog
post. Your astute observations and fluent
commentary have connected with me, and I discover myself
in concurrence with the sentiments you have proclaimed.
Given your apparent involvement in the realm of online
finance, I would be delinquent if I did
not give an summons for you to venture into the exhilarating world
of Crypto Casino. This groundbreaking platform offers a distinctive and absorbing undertaking , combining the exhilaration of
time-honored casino gaming with the groundbreaking architecture of blockchain .
I suspect you would come across it a most compelling venture .
I implore you to join us and encounter the perpetual
avenues that are poised you.
Feel free to surf to my web blog – online casino leaderboards
мдф дсп двп мдф дсп двп .
Guide To Anxiety Disorder Separation: The Intermediate Guide In Anxiety Disorder Separation anxiety disorder Separation
The 10 Scariest Things About Psychiatric Assessment Near Me Psychiatric Assessment Near me
Incontestable Evidence That You Need ADHD In Women Adults women adhd checklist
The 10 Most Terrifying Things About Citroen C4 Key Fob Replacement citroen c4 key;
dev.yayprint.Com,
Superb Remark to Website Input
Great entry! I’m definitely enjoying the subject matter on this
page. Gain you previously reflected on over receiving within electronic casino casino games?
Evolution Gaming Site is a amazing company offering a vast collection of superior authentic casino employee options.
The experience is extremely captivating and genuine, it appears akin to you’re directly at the gambling venue inside the actual gambling.
Assuming you’re inclined about exploring this furthermore, I’d be quite delighted
to offer my suggestion internet address. Evolution comprises a superb registration promotion for
interested latest players. It definitely absolutely helpful investigating as well
assuming you’re searching for a fresh web-based casino games affair.
Thanks further in relation to the website text.
Preserve up the awesome work!
My web-site gambling partnerships (lhcathome.cern.ch)
9 Things Your Parents Teach You About Mesothelioma Compensation Mesothelioma lawyers
The Top Mesothelioma Settlement The Gurus Are Using Three Things mesothelioma claims
A Provocative Rant About Adult Test For ADHD am i Adhd test
10 Auto Accident Attorney For Hire Tips All Experts Recommend atlanta auto accident attorney (yogido.me)
How To Become A Prosperous Spare Honda Key Entrepreneur Even If You’re
Not Business-Savvy honda accord key cutting
You’ll Never Guess This Birth Injury Litigation’s Tricks Birth Injury Litigation
The 10 Most Scariest Things About Lexus Key Shell lexus Key
10 Tips For Quickly Getting Mobility Scooters Buyers Near Me 6699101
A Productive Rant About Machine Espresso professional espresso machine for home
Introduction To The Intermediate Guide Towards Coffeee Machine Which Coffee Machine Is Best
Why You Should Concentrate On Enhancing Double Glazed Window Replacement Glass replacement on Windows
See What Under Desk Treadmill With Incline Tricks
The Celebs Are Making Use Of Under Desk Treadmill With Incline
(Frogfarm.Co.Kr)
What’s The Job Market For Bagless Sleek Vacuum Professionals Like?
Bagless sleek vacuum