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官方教學視訊,另有控制器指令介紹的專頁。
The Most Common Mistakes People Make With 4 Wheel Scooter 4 Wheel scooter for adults
Is Technology Making Birth Defect Legal Better Or Worse? lawsuit
What Freud Can Teach Us About Designer Handbags Large 836614
Search Engine Optimization – From Zero To Hero 구글 상위노출 백링크, https://voiceof.com,
Four Stuff You Should Be Aware Of About Slot Machines
에볼루션 오토배팅
Best For Win At Roulette 프라그마틱 체험 [http://loadtechnet.com]
Paypal The Poker Room – Which Poker Sites Accept Paypal?
다바오 유흥
14 Businesses Doing An Amazing Job At Slot Demo akun Demo wild bounty
Thank Your Opinions!
I’m Thrilled you Unearthed the Entry Beneficial.
If you’re Enthusiastic about Investigating more Choices in the online Wagering Domain, I’d Propose Visiting CMD368.
They Provide a Myriad of Exciting Punting Options, Livestream, and a Easy-to-navigate
Environment.
What I Especially Appreciate about CMD368 is their Dedication to Prudent Betting.
They have Robust Safeguards and Functionalities to Assist Participants Stay in control.
Regardless of whether you’re a Proficient Enthusiast or Inexperienced
in the Punting, I Reckon you’d Sincerely Relish the Exploration.
Don’t hesitate to Join Through the Link and Communicate if you have
Additional Inquiries.
Feel free to visit my blog; cmd368 singapore (emplois.fhpmco.fr)
The Biggest Sources Of Inspiration Of Asbestos Attorney 0270469.xyz
5 Lessons You Can Learn From Pavement Mobility Scooters Pavement Mobility Scooters For Sale
This Is The Ultimate Cheat Sheet For ADHD Diagnosis 9326527.xyz
Why Ford Car Key Replacement Isn’t As Easy As You Imagine price
What Consuming Know Before Applying For Can Make Loan 중기청 대출
How Slot Demo Gratis Pragmatic Play No Deposit Was The Most Talked About Trend In 2022
Demo slot Gates of olympus
What Is Wall Electric Fireplace? History Of
Wall Electric Fireplace In 10 Milestones Lynn Bolvin
Why Do So Many People Want To Know About Assessing Adhd In Adults?
adhd In Adults assessment
10 Semi Truck Tricks Experts Recommend legal
Erb’s Palsy Lawyers Tools To Make Your Daily Life Erb’s Palsy Lawyers Trick That Should Be Used By Everyone Know erb’s palsy lawyers
This Is A Under Desk Treadmill Uk Success Story You’ll Never
Be Able To Gym Equipment
Niche Marketing How To seo 백링크
The Most Effective ADHD Medication Adults Uk Tips To Change Your Life most Effective adhd medication – minecraftcommand.Science –
A Productive Rant About Locksmith For Car Near Me Elsy Crays
9 Lessons Your Parents Taught You About Mental Illness Assessment mental illness assessment
Seo – Cutting The Particular Clutter To Get You Results 백링크
Today, I went to the beachfront with my kids. I found a sea shell and gave
it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed.
There was a hermit crab inside and it pinched her ear.
She never wants to go back! LoL I know this is totally off topic but I
had to tell someone!
15 Things You’re Not Sure Of About Window Repair Near upvc window repair near Me
Three Of The Biggest Catastrophes In 3 Wheel Buggies History 435871.xyz
The Most Significant Issue With Cheap Sectional Sofas And How You Can Fix It 4452346
How Must Submit This Site To Search Engines? 검색엔진최적화 배우기