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,271 Responses

  1. uttehal表示:

    As a result of spin the nuclear particles act as small bar magnets. stromectol

  2. My coder is trying to convince me to move to .net from PHP. I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using WordPress on numerous websites for about a year and am nervous about switching to another platform. I have heard great things about blogengine.net. Is there a way I can import all my wordpress posts into it? Any help would be really appreciated!

  3. thich表示:

    Lucky niki casino no deposit bonus code “One of the ways we’re trying to surprise and delight players in the New Jersey market is with the introduction of a PalaCasino.com Slot Machine where players will be able to take a free spin every day for the chance to win a million dollars,” continued Ryan. “It’s differentiators like that that we think will appeal to players the most and set PalaCasino.com apart.” NJ Pala Casino is legal and authorized by the New Jersey Division of Gaming Enforcement as an internet gaming operator in accordance with the Casino Control Act N.J.S.A. 5: 12-1 and its implementing regulations. Our games are tested by the New Jersey Division of Gaming Enforcement. Pala Interactive LLC created its online casino in 2013, in anticipation of the launch of online gambling in the US. The online casino officially launched in New Jersey in 2014, following a successful testing period. Pala Casino operates under the majority ownership of the Pala Band of Mission Indians, with headquarters in Pala, California. It launched online in New Jersey via a partnership with the Borgata Hotel, Casino & Spa in Atlantic City. http://johnnyfyhx875420.blogkoo.com/top-no-deposit-bonus-casino-usa-31613200 The second part of the 888 Casino sign-up bonus works a little differently. We’re used to seeing 100% deposit offers in New Jersey, but 888 is offering a 120% bonus to new players when they make the first deposit. 888 also has a 30x requirement on these bonus funds, with 60 days for you to meet it. To exemplify, Apple iOS apps are often developed through Swift. The Swift programming language is aware of all the advantages and restrictions iPhones have. With that in mind, Swift developers know what to focus on and how to develop the best possible live casino app for iPhones. The main competitors, App Store and Google Play, first started their business around 2007 2008. iOS first ran an online casino app for Texas Hold’em, which was the most downloaded payment-required casino app from its launch.

  4. uttehal表示:

    Prescription Drugs buy ivermectin tablets Singapore Online Store Drug

  5. penpal表示:

    Developing a framework is important.

  6. Great post, keep up the good work, I hope you don’t mind but I’ve added on my blog roll.

  7. Guns For Sale表示:

    A great post without any doubt.

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

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

  10. Hi, do have a e-newsletter? In the event you don’t definately should get on that piece…this web site is pure gold!

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

  12. Nice piece of info! May I reference part of this on my blog if I post a backlink to this webpage? Thx.

  13. Gwenorepisn表示:

    casino free spins
    no deposit casino real money
    free sign up bonus online casino

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

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

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

  17. Sheelaghpisn表示:

    vpn book
    free vpn proxy video
    buy vpn for windows

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

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

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

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

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

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

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

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

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

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

發佈留言

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