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官方教學視訊,另有控制器指令介紹的專頁。
Keyword Density Tools – Why Feel Free To Use Keyword Density Tools
백링크 사이트
Can You Negotiate Cash Advances, Financial Information Or Utility Debt?
저신용자 대출
Don’t Be Enticed By These “Trends” About Replacement Land Rover Keys
emergency
Roulette Winning Systems – Do Discovered Exist? 슬롯
Small Internet Business Marketing – Does It Work?
구글 검색엔진최적화 (findchips.biz)
Great Rules To Choose A Car Loan 다바오 카지노 호텔
10 Things You Learned From Kindergarden That Will Help You With Mesothelioma Compensation Mesothelioma Claims
Do Not Believe In These “Trends” About Window Sash Replacement Replacement Glass For Windows Near Me
Why Do So Many People Would Like To Learn More About Cost Of Replacement Windows Uk?
window replacement Cost
15 Surprising Facts About Veterans Disability Attorneys Veterans disability law firms
The 10 Most Terrifying Things About Window Screen Replacement
window Screen replacement
10 Reasons Why People Hate Malpractice Lawsuit Malpractice
Lawsuit Malpractice lawyers
Tips For Understanding Baccarat 프라그마틱 순위
What Replacement Upvc Window Handles Experts Would Like You To Be Educated Upvc Window Repair Near Me – Bender-Delgado.Blogbright.Net –
The incredible story https://napoli.khvichakvaratskhelia-br.com of a young Georgian talent’s transformation into an Italian Serie A star. Khvicha Kvaraeshvili is a rising phenomenon in European football.
Keyword Research – The Right Way To Do It For Free! 검색엔진최적화 메타태그 (http://fasbender.net/__media__/js/netsoltrademark.php?d=posteezy.com/organic-seo-services-value-google-page-rank)
How To Beat Your Boss On Anxiety Disorder Physical Symptoms 5097533.xyz
The Significance Of Having Merely Keyword With Your Domain Name pbn 백링크 (bloq.biz)
10 No-Fuss Strategies To Figuring The SEO Experts London You’re Looking For local seo expert london
Five Killer Quora Answers To Strollers 3 Wheels 435871.xyz
Лучшие пансионаты для пожилых людей https://ernst-neizvestniy.ru в Самаре – недорогие дома для престарелых в Самарской области
14 Creative Ways To Spend Extra Money Cost Of Spare Car Key Budget http://www.99811760.xyz
The Most Hilarious Complaints We’ve Been Hearing About Spare Keys Cut http://www.99811760.xyz
Хотите научиться готовить самые изысканные и сложные торты? В этом https://v1.skladchik.org/tags/tort/ разделе вы найдете множество подробных пошаговых рецептов самых трендовых и известных тортов с возможностью получить их за сущие копейки благодаря складчине. Готовьте с удовольствием и открывайте для себя новые рецепты вместе с Skladchik.org
RioBet https://dacha76.ru
I discovered your blog post to be a intriguing and
insightful examination of the emerging state of the market .
Your analysis of the pivotal developments and
hurdles addressing companies in this sector was remarkably
powerful .
As an zealous supporter of this area, I would be delighted
to build upon this discussion more thoroughly .
If you are interested , I would happily entreat you to delve into the enthralling options
offered at WM CASINO. Our setting delivers a cutting-edge and safe landscape
for sharing knowledge with kindred spirit aficionados
and procuring a profusion of resources to elevate
your insight of this ever-changing sphere
. I anticipate the occasion of collaborating with you
in the foreseeable time
my web-site – online casino
Excellent article on the rise of digital gaming. As a person who is enthusiastic about this space, I value the in-depth information you
presented.
If anyone here is seeking a fresh option to try out, I’ve recently come across an online destination called BetCryptoCasino.net.
It features a wide range of casino offerings and sports
wagering options, utilizing virtual currencies for smooth and secure financial operations.
Feel free to investigate it and see if it suits your gaming preferences.
Cheers!
my site :: free crypto casino
Follow Bernardo Silva’s impressive career https://manchester-city.bernardosilva.net from his debut at Monaco to to his status as a key player and leader of Manchester City.
The fascinating story of Marcus Rashford’s rise https://manchester-united.marcusrashford-br.com from academy youth to the main striker and captain of Manchester United. Read about his meteoric rise and colorful career.
Credit Suisse Gold Bars – 5 Reasons Loan Company Says Buy 리드코프 무직자 대출