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...

14,017 Responses

  1. скорая наркологическая помощь в москве http://www.justforum.bestforums.org/viewtopic.php?f=26&t=4785/ .

  2. BrettTIT表示:

    Cheap Sildenafil 100mg: cheap viagra – viagra without prescription

  3. RogerFaf表示:

    https://fastpillseasy.com/# low cost ed meds

  4. BrettTIT表示:

    Buy Tadalafil 5mg: buy cialis online – cialis for sale

  5. fjwmwwwzl表示:

    Absolutely! The colorist will match the shade of the tint exactly with your existing eyebrow color so that no one will know you’ve had anything done except for how stunningly beautiful you now look with full and defined brows! Some of the technologies we use are necessary for critical functions like security and site integrity, account authentication, security and privacy preferences, internal site usage and maintenance data, and to make the site work correctly for browsing and transactions. Learn all about hybrid brows – a brow style made by combining microblading and microshading. The session is for clients whose brows have not completely faded to help maintain the appearance of your brows Your cart is currently empty. This technique is acceptable on most skin types except very oily.
    https://www.fundable.com/aaron-escobedo
    This felt-tip liquid liner is infused with plant-based ingredients like aloe and rosemary specifically to soothe delicate and sensitive eyes and skin. We love this eyeliner because it was actually dermatologist-developed! Liquid eyeliner can certainly be intimidating, which is the major reason why most makeup novices shy away from it. Fortunately, not all liquid eyeliners are as scary as they’re made out to be. SNAP THIS TO ADD US!@COLOURPOPCO “I’ve always been a liquid eyeliner girl since I mostly use eyeliner on my top lid. Most pencil eyeliners tend to catch on my skin and the line ends up looking chunky or broken. I have this liner in Brown(out) and am going to have to invest in a variety of colors. Besides the fact that this goes on SO smoothly, the shimmery colors are also light enough to use as a quick shadow. I use Brown(Out) on my top lid to enhance my eye color. I use a small amount on my bottom lid to create the “dewy eye” effect. Depending on the amount you put on, it is great smudged around and looks very sultry and not very harsh. First non-liquid eyeliner I’ve instantly loved. Would recommend this. Love it.”

  6. неотложная наркологическая помощь [url=www.kvitka.ukrbb.net/viewtopic.php?f=58&t=28107]неотложная наркологическая помощь[/url] .

  7. наркологическая скорая бесплатная [url=https://www.www.rolandus.org/forum/viewtopic.php?p=106508]наркологическая скорая бесплатная [/url] .

  8. неотложная наркологическая помощь в москве [url=https://www.taksafonchik.borda.ru/?1-2-0-00001312-000-0-0-1730876668]неотложная наркологическая помощь в москве[/url] .

  9. BrettTIT表示:

    where can i buy erectile dysfunction pills: cheap cialis – cheap erectile dysfunction pills

  10. Barrybib表示:

    erectile dysfunction medication online: edmeds – ed meds online

  11. Donaldabula表示:

    ed online meds fast pills easy top rated ed pills

  12. Donaldabula表示:

    Viagra online price FastPillsForMen best price for viagra 100mg

  13. 10 Things That Your Family Teach You About Bi Fold Door Repair Near Me bi fold door repair Near me

  14. Charlesbot表示:

    https://maxpillsformen.com/# Tadalafil Tablet

  15. RogerFaf表示:

    http://fastpillsformen.com/# Cheap generic Viagra

  16. RogerFaf表示:

    https://maxpillsformen.com/# Cialis over the counter

  17. RogerFaf表示:

    https://fastpillsformen.com/# buy viagra here

  18. Charlesbot表示:

    https://fastpillseasy.com/# ed pills cheap

  19. BrettTIT表示:

    cheapest online ed meds: cheap cialis – online ed medications

  20. Barrybib表示:

    buy Viagra over the counter: buy viagra online – Generic Viagra for sale

  21. RogerFaf表示:

    http://maxpillsformen.com/# cheapest cialis

  22. BrettTIT表示:

    Generic Cialis without a doctor prescription: Tadalafil Tablet – Generic Cialis without a doctor prescription

  23. RogerFaf表示:

    http://fastpillseasy.com/# buy erectile dysfunction pills online

發佈留言

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