AngularJS – Controller 介紹篇

繼續AngularJS的介紹,接下來我們看看controller的應用,以商品介紹的頁面為例,來看看這些資料要如何呈現在頁面上,想要呈現的資料有產品名稱、價格以及描述。

為了方便文章閱讀,本篇將application翻譯成應用程式;controller翻譯成控制器

AngularJS Part2 Slide1

控制器負責定義函數和值來決定整個應用程式的行為,同樣我們把Javascript和HTML分開來開發,在app.js內定義應用程式名稱和控制器的名稱、函數與值,在這次的案例中也提到利用(function(){})()包裝整個javascript是一種好習慣,有關於函式的基本觀念,大家可以閱讀一下這篇文章,介紹的還蠻詳細的!包裝部分的概念可參考其中介紹的立即函示(IIFE – Immediately Invoked Function Expression)。

AngularJS Part2 Slide2

定義好之後可以開始定義該控制器內的值,其中this.product=gem的意思是指未來在輸出時可以利用product.name去取得Dodecahedron;product.price去取得2.95…等等。

AngularJS Part2 Slide3

接下來就是把這些值顯示在網頁上,首先可以在div標籤內加上ng-controller=”StoreController as store”的屬性,其中ng-controller是去指定StoreController為該div內的控制器名稱;store是別名(類似綽號)未來呼叫store時就是在呼叫StoreController這個控制器。

AngularJS Part2 Slide5

在HTML標籤內開始輸出該控制器內的值,分別是:store.product.name、store.product.price、store.product.description三個項目,而在頁面上就會看到這三個值被顯示出來了。

AngularJS Part2 Slide6

需要注意的是,如果我們把輸出值部分的程式碼放在div外側的話,會沒有辦法順利的顯示值,因為我們的控制器是指定於div標籤內,離開了div標籤就無法取得該控制器了。

AngularJS Part2 Slide7

本篇的教學就到這邊,大家也可以透過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官方教學視訊,另有控制器指令介紹的專頁

You may also like...

9,252 Responses

  1. Darsiepisn表示:

    dissertation writing tutor
    writing doctoral dissertation
    find a dissertation

  2. Sallipisn表示:

    dissertation online help
    dissertation write up
    help tutor

  3. Suzannpisn表示:

    define dissertation
    art dissertation help
    cheap dissertation help in los angeles

  4. Glynnispisn表示:

    dissertation editing
    dissertation acknowledgement sample
    dissertation title examples

  5. Verieepisn表示:

    +writing help
    dissertation search
    what is a dissertation

  6. Whats web pro表示:

    Thank you so much for sharing this wonderful post with us.

  7. This blog post is excellent, probably because of how well the subject was developed. I like some of the comments too.

  8. Amazing content on your website.

  9. Dortheapisn表示:

    doctoral dissertation help your
    how to write a dissertation
    dissertation writing tips

  10. whats web表示:

    Nice i really enjoyed reading your blogs. Keep on posting. Thanks

  11. The information shared is of top quality which has to get appreciated at all levels. Well done…

  12. Avivapisn表示:

    dissertation help online free
    dissertation help
    example of dissertation

  13. Thank you for sharing indeed great looking !

  14. I think it is a nice point of view. I most often meet people who rather say what they suppose others want to hear. Good and well written! I will come back to your site for sure!

  15. jasa backlink表示:

    Nice i really enjoyed reading your blogs. Keep on posting. Thanks

  16. Chandapisn表示:

    phd dissertation writing
    writing a master’s dissertation
    writing a dissertation in a day

  17. agensi.co.id表示:

    A great post without any doubt.

  18. A friend of mine advised me to review this site. And yes. it has some useful pieces of info and I enjoyed reading it.

  19. Thivy表示:

    You must be at least 18 years old to visit this website. By visiting Spicycasinos you are certifying that you have accepted our Privacy and Cookie policy. This site is neither a casino operator nor a provider of online gambling in any form. The online casino usually processes all withdrawal notices within 2 to 4 days, with the exact timeframes being dependent on the player’s verification status. Anyway, numerous pieces of feedback show that players tend to trust the casino and its withdrawal procedures which are professional, fairly fast and always honest with no unexpected strings. We believe so! Jambo Casino offers an enticing selection of casino games including video slots, table games, jackpots, live casino and scratchcards. It also provides players with a generous welcome bonus, a fun VIP scheme and excellent customer support. What more could you want? For more details, check out our full Jambo Casino review. https://careforfuture.org.uk/Forum/profile/jedphd62807904 The restaurants, bars and hotel — where the cheapest room available this Saturday night is $1,199 — opened in December, while the casino licence was pending. The hotel has a luxurious spa set over 2000m2, Crown Spa Sydney features “relaxation rooms, vitality pools, experience showers, infra-red saunas” as well as terrace areas for yoga. Fair GO is a lovely online casino that caters mostly to Australian players and invites them to play in their spare time. The site’s lucky mascot Koala greets visitors and directs them to the ideal gaming lobby, which includes slot machines, table games, slot machines, video poker, specialty, and progressive players. Copyright В© 2016 GlobalCasinoGuide.com XML Sitemap orientxpresscasino.com The list of best Australian casinos 2022 Take a minute to subscribe to our monthly newsletter featuring hospitality stories and trends.

  20. Great blog here! Also your site loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my website loaded up as fast as yours lol

  21. Hello. Great job. I did not expect this on a Wednesday. This is a great story. Thanks!

  22. If you don’t mind, where do you host your weblog? I am looking for a very good web host and your webpage seams to be extremely fast and up most the time…

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。