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官方教學視訊,另有控制器指令介紹的專頁。
A thoughtful insight and ideas I will use on my blog. You’ve obviously spent some time on this. Congratulations
Nice i really enjoyed reading your blogs. Keep on posting. Thanks
The information shared is of top quality which has to get appreciated at all levels. Well done…
Hello there! I really enjoy reading your blog! If you keep making amazing posts like this I will come back every day to keep reading.
Sweet blog! I found it while browsing on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Thank you
If you are planning a major remodel, determine the new layout of your kitchen before purchasing new kitchen appliances to make sure they will fit the space. If you’re planning a kitchen remodel, the truth is, there’s no way to avoid at least some inconveniences. Depending on the scale of your project, it’s going to be a pain to live through. Yet, with a little preparation, the there are ways to survive during this testing time. Enter the temporary kitchen. A super-sub in times of construction war. Our entire kitchen is only 120 square feet. For years, we daydreamed about extending it onto a small unused patio, but finally conceded that was a budget buster. By maximizing the space we had (plus the extra cabinet space from raising the ceiling, etc.) this remodel made our small kitchen feel surprisingly roomy, and the efficient layout has made cooking much less stressful. I made brunch for my visiting parents without breaking a sweat and had friends over for coffee before the construction dust had cleared. Our daughter loves the space; she calls it “her” kitchen. And those cupcakes? Terrific. https://kenoshaultralightclub.org/community/profile/boydconnolly214 At this point we are around $40,000 in total cost for the basement space of 400 square feet. That is about $100 per square foot just for the site work, excavation and concrete work for the basement. This is before you add window wells, stair access, and utilities, or other aspects to finishing the interior of the basement to make it usable space. Homeowners who cannot afford natural lighting in the basement beyond the statutory regulation adopt artificial lighting. Install brighter lights where safety and visibility are important, for example in the kitchenette, corridor, staircase, library, gym, and playroom. Install dimmed or soft lights in areas where ambience is important, for example the bar, home theatre, music and art exhibition room, bedroom, and sauna. Use shades and ceiling reflectors to reduce illumination. Install automatic, low-power lights in closets and cabinets. A set of light fixtures, reflectors, and shades cost about $200 while a simple chandelier cost less than $100.
I concur with your conclusions and will eagerly look forward to your future updates. The usefulness and significance is overwhelming and has been invaluable to me!
Amazing article, cheers, I will bookmark you now.
I wish I could craft such articles as this. Thank you very much.
This is really interesting, You’re a very skilled blogger. I have joined your feed and look forward to seeking more of your great post. Also, I’ve shared your web site in my social networks!
slot machines
free slots that pay cash
slots online
Thank you for sharing indeed great looking !
free cats slots
ashbringer relic slots
free adult slots online
I truly appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Bing. You have made my day! Thank you again
https://diamondhairs.com.ua/investirovanie/
1b15fa54f8 xenpalm
I’ve been absent for a while, but now I remember why I used to love this web site. Thank you, I will try and check back more often. How frequently you update your site?
Easily, the post is really the greatest on this laudable topic. I concur with your conclusions and will thirstily look forward to your future updates. Saying thank will not just be sufficient, for the wonderful c lucidity in your writing. I will instantly grab your rss feed to stay privy of any updates. Solid work and much success in your business enterprise!
I really believe you will do well in the future I appreciate everything you have added to my knowledge base.
Great post. Just a heads up – I am running Ubuntu with the beta of Firefox and the navigation of your blog is kind of broken for me.
This is one very informative blog. I like the way you write and I will bookmark your blog to my favorites.
Dealzclick is one stop shop with the best online shopping deals today, offering Best travel deals and Best online webhosting dealsfrom trusted best sellers. We are all about deals.
Excellent article!! I am an avid reader of your website:D keep on posting that good content. and I’ll be a regular visitor for a very long time!!
This website has lots of really useful stuff on it. Thanks for informing me.