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官方教學視訊,另有控制器指令介紹的專頁。
Do Not Buy Into These “Trends” Concerning Private ADHD Assessment Liverpool Cost adhd assessment scotland Private
What Freud Can Teach Us About Accident Lawyer Savannah
Attorney Accident Lawyer (https://Mozillabd.Science)
17 Signs That You Work With Key Programmers key Programmers near me
Beware Of These “Trends” Concerning Electric Fireplace Wall Mounted
ultra slim wall mounted electric fires (Oliva)
See What Best Places To Buy Bunk Beds Tricks The Celebs Are Using
Best Places To Buy Bunk Beds
Solutions To The Problems Of Get Diagnosed With ADHD private adhd Diagnosis london
Are You Getting Tired Of Diagnosis For ADHD?
10 Inspirational Ideas To Revive Your Passion How Do I Get Diagnosed For Adhd, http://Dudoser.Com/User/Toastjapan35/,
Three Reasons Why 3 Reasons Why Your Real Leather Sofa Is
Broken (And How To Fix It) leather corner sofa Sale
What’s Everyone Talking About Wall Mount Fireplace Today Fireplace Insert (Bookmarkinginfo.Com)
Three Greatest Moments In Ghost Immobiliser Birmingham History autowatch ghost installers west midlands (Mable)
Сервисный центр предлагает ремонт электросамокатов speedroll в москве ремонт электросамокатов speedroll
I uncovered your blog post to be a captivating and
perceptive examination of the contemporary state of the industry .
Your investigation of the primary developments
and difficulties tackling establishments in this realm was
outstandingly powerful .
As an passionate supporter of this area, I would be thrilled
to continue this discussion in greater detail .
If you are interested , I would enthusiastically implore you to delve into
the captivating opportunities offered at WM CASINO.
Our space provides a high-tech and fortified environment for networking with like-minded adherents
and attaining a profusion of materials to fortify
your grasp of this evolving sphere . I eagerly await the occasion of partnering with you in the foreseeable
time
This History Behind Foldable Treadmill Will Haunt
You For The Rest Of Your Life! foldable treadmill Uk
20 Resources To Make You Better At Mesothelioma Law mesothelioma Case
пансионат для престарелых в андреевке xn—-1-6cdshb2cetnmcmp6c5f.xn--p1ai .
10 Quick Tips On Pushchairs Prams Twin Pushchairs
The 10 Scariest Things About L Shaped Couch Small l shaped couch small
заказать дипломную работу недорого arusak-diploms.ru .
9 Things Your Parents Teach You About Mesothelioma Lawsuit
mesothelioma lawsuit (Candice)
государственный пансионат для престарелых инвалидов государственный пансионат для престарелых инвалидов .
10 Glass.Replacement Near Me Tips All Experts Recommend Company
Fake Shoes ma099
discountshoesmart,Replica Sneakers for women rw564
Is Bandar Toto The Greatest Thing There Ever Was? togel
Singapore (https://maps.google.com.qa/url?q=https://click4r.com/posts/g/17690627/unquestionable-evidence-that-you-Need-situs-terpercaya)
10 Myths Your Boss Has Concerning Depression And Anxiety Treatment Bipolar Depression treatment
10 Things Everyone Hates About Gorenje Fridge Freezer Retro retro fridges and
freezers (Reva)
The Most Significant Issue With Tiny Pellet Stove And
How You Can Solve It Small Pellet Stoves
10 Things That Your Family Taught You About Test For Adult ADHD test for adult adhd
The 12 Worst Types Of Users You Follow On Twitter wheelchair car ramps (Paulina)
купить дипломы техникума цена московская область russa-diploms.ru .
Retro Fridge Freezer Uk Tips To Relax Your Everyday Lifethe Only Retro Fridge Freezer Uk Trick That Everyone Should Be Able To retro fridge freezer uk