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官方教學視訊,另有控制器指令介紹的專頁。
See What Window Repairs Near Me Tricks The Celebs Are Utilizing window repairs near me (http://aragaon.net/bbs/board.php?bo_table=review&wr_id=401699)
Guide To Anxiety Treatment Medicines: The Intermediate Guide Towards Anxiety Treatment Medicines Anxiety treatment medicines
5 Killer Quora Answers On Cheap Single Pushchair Cheap Single Pushchair
15 Current Trends To Watch For Slot Sugar how to stop Sugar
rush immediately (krotcinus.com)
Many Of The Common Errors People Make With Double Glazed Window Repair Near
Me fix
An Guide To Personal Injury Lawsuit In 2023 Personal Injury Law Firms
เรียกเช็ค ข้อมูลสถิติ การเล่น คาสิโนแบบออนไลน์ cmd 368 และเจอกับ ประสบการณ์ที่ดีมาก ในคาสิโนชั้นนำของเว็บไซต์
cmd368 เพื่อให้คุณได้เพลิดเพลินกับการเล่นคาสิโนที่มีผู้เข้าชมมากที่สุดและรับรองความปลอดภัย ร่วมท้าทายโชคชะตารุ่งเรืองและได้กลมกลืนกับประสบการณ์คาสิโนที่ไม่ว่าจะเป็นผู้เล่นใหม่หรือผู้เล่นมืออาชีพ.
my site: การพนันคาสิโนออนไลน์, humanlove.stream,
Double Glazed Window Repair’s History Of Double Glazed Window Repair In 10 Milestones
Lottie
Make Your Writing Or Marketing Projects Your The Main Ageda 슬롯
Could Kids Bunk Bed With Stairs Be The Key To Dealing With 2023?
Kids Bedroom Furniture (https://Links.Gtanet.Com.Br)
7 Simple Tricks To Moving Your Trucking Lawyers truck accident law firm
10 Healthy Window Repair Luton Habits Windows Company Near Me
See What Truck Accident Lawsuit Tricks The Celebs Are Utilizing truck accident lawsuit (dlibra.bibliotekaelblaska.pl)
30 Inspirational Quotes About Trucking Lawyers Truck accident
Choosing Best Online Casino Is Difficult But Rewarding 프라그마틱 정품확인
9 Lessons Your Parents Taught You About Double Glazed Window Suppliers Near
Me double glazed window suppliers near me
5 Laws That Anyone Working In Malpractice Attorneys Should Be Aware Of Malpractice law firm
Five Killer Quora Answers To Mesothelioma Compensation Mesothelioma Compensation
10 Things That Your Family Taught You About Window Fixer London window fixer London
The 10 Most Scariest Things About Upvc Doors Near Me upvc door, https://santecosmetic.co.kr/bbs/board.php?bo_Table=free&Wr_id=27043,
Top Single Strollers: What Nobody Has Discussed single gb stroller
Railroad Injuries Settlement Tips To Relax Your Everyday Lifethe Only Railroad Injuries Settlement Trick That Should Be Used By Everyone Know Railroad Injuries
Bedford Windows And Doors Tools To Streamline Your Daily Life Bedford Windows And Doors Trick That Everyone Should Learn bedford windows
Five Killer Quora Answers On Window And Door
Replacement London Window and door replacement london
The Top Companies Not To Be Follow In The Tree House Twin Bunk
Bed Industry tree house loft bed
Paul Labille Pogba https://pogba.com.az French footballer, central midfielder of the Italian club Juventus. Currently suspended for doping and unable to play. World champion 2018.
Five Killer Quora Answers To Replacement Double Glazing Units Near Me replacement double glazing units near me (Lee)
20 Things You Need To Know About Federal Employers Federal Railroad
Greetings, peer enthusiast. I must applaud you on your discerning analysis of the subject matter presented in this blog post.
Your sharp observations and lucid commentary have
connected with me, and I conclude myself in agreement
with the feelings you have proclaimed.
Given your discernible engagement in the realm of virtual finance,
I would be delinquent if I did not present an solicitation for you to venture into the
exhilarating world of Crypto Casino. This groundbreaking platform offers
a unique and spellbinding endeavor, blending the rush of
customary casino gaming with the advanced solutions of
distributed ledger . I think you would discover it a most captivating venture .
I invite you to join us and unveil the
boundless avenues that are poised you.
Feel free to surf to my page … online casino customer acquisition
The Unknown Benefits Of Upvc Door Panels replacing A upvc door lock