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官方教學視訊,另有控制器指令介紹的專頁。
Indisputable Proof That You Need Fold Flat Treadmill https://www.zackfoxworth.top/hm479x-4qn16v-2h2fi-is5-4rl-2714/
5 Laws To Help The Double Glazed Windows Crawley Industry hotel
glass repair (Georgina)
Completed Reading a Blog Post: A Formal Feedback to the Comment
Section and an Invitation to Join “KING855”
‘After rigorously studying the blog post, I would like to furnish the following reply to
the forum.
Your perspectives concerning the theme were quite intriguing .
I was in consensus with several of the assertions you articulated.
It is gratifying to see such an animated dialogue unfolding.
If you are inclined in additional delving into this
issue , I would cordially invite you to become a member of the “KING855” network .
In that space, you will have the opportunity to engage with kindred spirit individuals and delve deeper into these intriguing
subjects.
I am convinced your participation would be a valuable enhancement to the
dialogue.
Appreciate your remarks, and I anticipate the possibility
of continuing this enlightening exchange .
Also visit my blog … online casino regulatory compliance
Search Engine Optimization As A Means To Targeted Traffic 검색엔진최적화 마케팅 [emplois.fhpmco.fr]
How Do You Explain Double Glazed Windows High Wycombe To A Five-Year-Old doors high wycombe
10 Quick Tips For Upvc Windows And Doors Repair Upvc Window
Accident Lawsuit’s History History Of Accident Lawsuit accident lawyers
The Reasons Cost To Replace Windows Uk In 2023 Is The Main Focus
Of All People’s Attention. 2023 windowreplacement
10 Things We Hate About Double Glazed Window Replacement Near Me double hlazing
– Carma,
5 Killer Quora Answers On Window Doctor Near Me Window doctor near me
10 Reasons That People Are Hateful To Childrens Bunk Bed Childrens Bunk Bed eddafay.top
Five Lessons You Can Learn From Upvc Window Repairs
upvc windows Repair
15 Gifts For The Upvc Door Locking Mechanism Lover In Your Life replacement panels for upvc
doors (Fleta)
10 Untrue Answers To Common Workers Compensation Litigation Questions Do
You Know The Correct Answers? workers’ compensation law firm
There’s A Reason Why The Most Common Birth Defect Attorney Debate Actually Isn’t As
Black And White As You Might Think birth defect law firm
20 Quotes Of Wisdom About Malpractice Attorneys
Malpractice law firm
Salutations , fellow observer . I came across your deep input on the blog write-up
incredibly keen .
Your position on the subject matter is considerably
impressive .
As you give the impression to maintain a ardent interest in the topic , I aim to give an solicitation for you to dive into the
domain of ‘918KISS’.
This exclusive website furnishes a vast variety of riveting resources that cater to
people comprising eclectic passions .
I believe you might find the group at ‘918KISS’ as being equally enlightening
and academically stimulating .
I urge you to ponder becoming a member of us and providing your peerless judgments to
the uninterrupted exchanges . Eagerly awaiting potentially admitting you
as a member .
Here is my homepage; online casino VIP programs
Why Truck Injury Attorneys Is The Best Choice For You?
firms
Initial Seo Steps For Link Building 구글상위노출 seo작업
10 Locations Where You Can Find Double Glazing Windows Cambridgeshire
Double Glazed Door Repairs
Double Glazing Near Me: 11 Things You’re Leaving Out Double Glazing company near Me
20 Things You Must Be Educated About Semi Truck Legal semi truck accident lawsuits (edupass.co.Za)
Wow, this was a really educational and exhaustive write-up on the sphere of virtual gaming venues !
As someone who’s been exploring the domain of decentralized blockchain-powered gambling , I really acknowledge you tackling all the key
qualities – from the outstanding digital casinos
with ample rewards and exclusive features , to the
emergence of private choices for greater security.
It’s superb to see how this market is never-ending
progressing to fulfill the demands of the rising thirst .
I appreciate for assembling such a comprehensive and informative reference – it’s exactly the nature of content I was
seeking to uncover the thrilling blockchain-based betting house
ecosystem . Cheers !
my blog; online casino loyalty programs (robertchang.ca)
10 Facts About 18 Wheeler Wreck Lawyer That Insists On Putting You In A Good Mood 18 wheeler Accident
See What Window Repairs Near Me Tricks The Celebs Are Making
Use Of window repairs near me (fourtoons.com)
20 Quotes That Will Help You Understand 18 Wheeler
Truck Accident Attorney 18 wheeler Accident Lawyer
You’ll Never Be Able To Figure Out This Upvc Window Repair Near Me’s Benefits upvc Window repair Near me
Window Repair Near Me Tools To Improve Your Daily Lifethe One Window Repair Near Me Trick
That Should Be Used By Everyone Be Able To window repair Near me
Watch Out: How Accident Legal Is Taking Over And What You Can Do About It lawyer
15 Gifts For The Volvo Keyfob Lover In Your Life Volvo key repair