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官方教學視訊,另有控制器指令介紹的專頁。
I have meticulously reveled in the musings provided in this
contemplative blog article . The scribe has fluently
conveyed several fundamental notions that strike a chord
with me powerfully .
As an fervent advocate of innovative
commercial projects , I would yearn to extend an suggestion to you to investigate the remarkable opportunities available at Pragmatic Play .
This energetic entity is at the van of pioneering leaps,
affording a effervescent and rewarding realm
for enthusiasts who exhibit a fervor for preeminence
and a grit to push the confines of what is realizable.
I enjoin you to mull over this proposal and investigate the profusion of
openings that present themselves . Kindly be at liberty
to query if you have any inquiries or would aim to mull over in greater
detail .
Most heartfelt salutations ,
Feel free to visit my web page; gambling affiliate marketing
cost estradiol – femara pills buy arimidex generic
Salutations , companion viewer . I came across your insightful
commentary on the blog entry exceptionally perceptive .
Your position on the matter is significantly noteworthy .
As you appear to bear a passionate interest in the content ,
I aim to provide an invitation for you to dive into the expanse of ‘918KISS’.
This particular hub presents a broad array of
absorbing data that in turn accommodate
persons including heterogeneous passions .
I gather you will uncover the group at ‘918KISS’ as
equally enlightening and cognitively riveting
.
I suggest you to reflect on becoming a member of us and
sharing your precious analyses to the ceaseless
dialogues . Eagerly awaiting hypothetically
accepting you aboard .
Here is my blog … online roulette
With huge multipliers and quick decision-making, Aviator has become a favorite among Indian online players. Use the demo to master your skills, then cash out in real money play before the plane flies away!
aviator game online [url=https://aviator-games-online.ru/]aviator game real money[/url] .
Тут можно преобрести сейфы для оружия цены купить шкаф для оружия
Тут можно преобрести сейф охотничий купить сейф под оружие цена
вывод из запоя стационарно ростов https://klin.0pk.me/viewtopic.php?id=4405 .
вывод из запоя цены ростов tatuheart.ukrbb.net/viewtopic.php?f=8&t=15065 .
вывод из запоя ростов на дону https://www.pokupki.bestforums.org/viewtopic.php?f=7&t=24037 .
онлайн казино беларусь онлайн казино беларусь .
вывод из запоя на дому http://kyevlyn.ukrbb.net/viewtopic.php?f=2&t=13619/ .
вывод из запоя ростов вывод из запоя ростов .
вывод из запоя на дому http://pokupki.bestforums.org/viewtopic.php?f=7&t=24037/ .
казино беларусь казино беларусь .
вывод из запоя ростов на дону стационар http://www.kyevlyn.ukrbb.net/viewtopic.php?f=2&t=13619/ .
вывод из запоя стационар ростов http://www.ximki.ukrbb.net/viewtopic.php?f=12&t=3698/ .
вывод из запоя круглосуточно ростов-на-дону zarabotokdoma.creartuforo.com/viewtopic.php?id=11473 .
With huge multipliers and quick decision-making, Aviator has become a favorite among Indian online players. Use the demo to master your skills, then cash out in real money play before the plane flies away!
aviator real money game [url=https://aviator-games-online.ru/]aviator game online[/url] .
Тут можно преобрести несгораемый сейф купить купить противопожарный сейф
Как официально купить аттестат 11 класса с упрощенным обучением в Москве
Experience the thrill of Aviator—a game built on timing and strategy. Players in India can explore this unique casino game in free demo mode before going for real money. Cash out before the plane crashes and maximize your wins!
avaitor game [url=https://aviator-games-online.ru/]play aviator online[/url] .
Тут можно преобрести огнеупорный сейф сейфы от пожара
Тут можно преобрести сейфы пожаростойкие купить сейф несгораемый
Как официально купить диплом вуза с упрощенным обучением в Москве
Тут можно преобрести шкаф для ружья сейф для охотничьего ружья купить
Как быстро и легально купить аттестат 11 класса в Москве
Узнайте стоимость диплома высшего и среднего образования и процесс получения
I have painstakingly delighted in the insights provided
in this insightful blog article . The penman has fluently expounded several fundamental ideas that resonate with me significantly .
As an zealous crusader of cutting-edge entrepreneurial endeavors , I would like to offer an appeal to you to investigate the exceptional possibilities available at Pragmatic Play
.
This dynamic company is at the vanguard
of technological innovations , providing a lively and
rewarding ecosystem for persons who possess a passion for pinnacle and a determination to exceed the
confines of what is viable .
I enjoin you to consider this suggestion and explore the profusion of chances that lie in store .
Respectfully do not hesitate to connect if you have any inquiries or would intend to deliberate on further .
Most respectful regards ,
Feel free to visit my homepage – roulette
Здесь можно преобрести купить сейф в москве сейф купить москва
Всё, что нужно знать о покупке аттестата о среднем образовании без рисков