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官方教學視訊,另有控制器指令介紹的專頁。
Центр сертификации https://www.rospromtest.ru осуществляет деятельность по содействию в подтверждении соответствия продукции и услуг требованиям нормативных документов, технических регламентов Таможенного союза, и сертификации ISO. Мы оказываем полный комплекс услуг в сфере сертификации.
Paying For College – How To Manage A Student’s First
Visa Or Mastercard 다바오 포커 모바일
What’s The Current Job Market For Chaise Lounge With Storage Professionals Like?
chaise lounge with storage – sobrouremedio.com.br –
What NOT To Do During The Window Replacement Industry Windows replacement near me
You’ll Never Guess This 18 Wheeler Accident Law Firm’s Tricks 18 Wheeler Accident Law Firm
Why We Our Love For Coffeee Machine (And You Should Too!) which coffee machine
is Best (moneyasia2024visitorview.coconnex.com)
A Journey Back In Time: How People Talked About Demo Slot Zeus Vs Hades Rupiah 20 Years Ago oscarreys
Get Personal Loan Questions Answered Here 100만원 대출 (Lawofpolkcounty.Com)
A Provocative Remark About Boot Mobility Scooter Arlen Nizo
What Find Out Before An Individual Personal Loans 대출 (http://cowdenllc.com/__media__/js/netsoltrademark.php?d=ringstreet.co/__media__/js/netsoltrademark.php?d=okbaroloan.com)
Beware Of These “Trends” About Collapsible Mobility Scooter used Car boot Mobility Scooters
10 Things Everybody Has To Say About Multifuel Stove 349338 –
Amelie –
What’s The Current Job Market For Lightweight Folding Mobility Scooters Professionals Like?
lightweight folding mobility scooters
Ten Things You Learned In Kindergarden That’ll Help You With Four Wheel Motor Scooter four Wheel scooters – willysforsale.com –
This Is The One Coffee Machines Trick Every
Person Should Know Coffee Pot Machine
What’s The Current Job Market For Double Glazed Window Repairs Near Me Professionals
Like? window Repairs Near me
The 10 Most Scariest Things About Hire Car Accident Lawyers car Accident lawyers
Wheelchair Ramps For Steps: The Secret Life Of Wheelchair Ramps For Steps http://Www.1109278.Xyz
A. The Most Common Bentley Valet Key Debate Isn’t As Black
Or White As You Think Bentley key replacement
5 International SEO Services Lessons From The Pros local seo services
Guide To Couches L Shape: The Intermediate Guide
The Steps To Couches L Shape Couches L Shape
I discovered your blog post to be a stimulating and perceptive examination of the present state of
the field . Your investigation of the essential movements and issues encountering
companies in this realm was strikingly persuasive .
As an ardent adherent of this area, I would be to build upon
this deliberation more extensively . If you are inclined , I would cordially invite
you to investigate the captivating opportunities open at WM CASINO.
Our framework provides a cutting-edge and secure
realm for connecting with compatible individuals and securing a wealth of resources to
bolster your awareness of this evolving landscape . I await with excitement
the occasion of joining forces with you in the impending time
Review my site click here
What’s Next In Nissan Key Replacement Prices Cheap
7 Effective Tips To Make The The Most Of Your Freezers For Garages Fridge Freezer
Deals (http://Www.Annunciogratis.Net/Author/Locustanimal23)
The Reasons Coffee Machines Espresso Is Everywhere This Year best ground coffee For espresso machine
AI Nude Generator https://ainudegenerator.app Transform Photos with AI Nude App.
GSA SER Backlinks Tools To Improve Your Daily Life GSA SER Backlinks Trick That Every Person Should
Be Able To gsa ser Backlinks
You’ll Be Unable To Guess Panels For Upvc Doors’s Tricks upvc Door replacement lock
You’ll Never Guess This Upvc Window Repair Near Me’s Secrets Upvc Window Repair Near Me
5 Laws That Can Help The Jaguar Key Case Industry Jaguar xf smart Key