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官方教學視訊,另有控制器指令介紹的專頁。
Thank you so much for sharing this wonderful post with us.
Nice i really enjoyed reading your blogs. Keep on posting. Thanks
Nice i really enjoyed reading your blogs. Keep on posting. Thanks
https://wakelet.com/wake/NP6DfB-WAsALDTUAgihBV
e246d94438 reenluc
https://wakelet.com/wake/E–7mkoEWytyPLecnYglH
e246d94438 walhenr
https://wakelet.com/wake/WYnxY_coMPWFGhO4PBtJ5
e246d94438 latoord
https://wakelet.com/wake/ao3pD1jXHcNN1K-FIoobJ
e246d94438 halbir
https://wakelet.com/wake/MSd4j5-xHbK0FnJdUHsPr
e246d94438 marlmak
https://wakelet.com/wake/uWTjmXnCvj81fi4emC1ef
e246d94438 earyil
https://wakelet.com/wake/1QmOusgQKTr5SDsUpqZ2B
e246d94438 ormoqua
https://wakelet.com/wake/0ZTHHuQHguGFTfTjZ3LCD
e246d94438 manapp
Dignity Educational CNA School Consultants offer a variety of services including curriculum writing, school catalog preparation, clinical externship contracts preparation, business plan writing, school policies preparation, student handbooks, and provide some other needed forms. We stick with you through approval and 6 months after, depending on your package. Dignity Educational Consulting helps our clients to start up nursing assistant schools in the 50 states.
https://wakelet.com/wake/VVbMnTg2d7iaQ5SGXvT3d
e246d94438 deilev
Lovely just what I was looking for. Thanks to the author for taking his clock time on this one.
https://wakelet.com/wake/GRdrqZDP9b9iIolbAE82O
e246d94438 quangenn
Thank you for sharing indeed great looking !
https://wakelet.com/wake/KmJqBpuhJFUzP_HJWZWN5
e246d94438 prisnir
https://wakelet.com/wake/0Kh2z4GyjAqq6Lw6Qm4NJ
e246d94438 vanros
https://wakelet.com/wake/3oSqLWWttXtG02wgtDRJK
e246d94438 desemi
https://wakelet.com/wake/oXSpA7E7umAE8a9wJfEM6
e246d94438 marbel
https://wakelet.com/wake/kK7R2pq9zf1-coJP5E4tq
e246d94438 valkac
https://wakelet.com/wake/CeyDp7ZSGsFc2177axccW
e246d94438 femiral
https://wakelet.com/wake/Tjt8oWeM4mIaE13txpD_U
e246d94438 okicon
https://wakelet.com/wake/Yu-QWCOIvulsIxjHVcb89
e246d94438 jessflor
https://wakelet.com/wake/1hZU2Z2RN-u_raLwvC8PU
e246d94438 naival
https://wakelet.com/wake/Hg7FaOeBOP_TU_FaKxA18
e246d94438 rowymart
https://wakelet.com/wake/Svu-P9yCSlvCLfxmdDO2e
e246d94438 morfaxa
https://wakelet.com/wake/eghffXr8ouWR8UhmAI_wZ
e246d94438 jamephi
https://wakelet.com/wake/B_Ei3FQBvMz50jQDP2WYX
e246d94438 aleeher
https://wakelet.com/wake/fq9arq7I3rPBfSUqhnSsE
e246d94438 hamayle