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

  1. smiqzzel表示:

    hydroxychloroquine over the counter canada does hydroxychloroquine require a prescription

  2. A cool post there mate ! Thank you for posting.

  3. Your thing regarding creating will be practically nothing in short supply of awesome. This informative article is incredibly useful and contains offered myself a better solution to be able to my own issues. Which can be the specific purpose MY PARTNER AND I has been doing a search online. I am advocating this informative article with a good friend. I know they are going to get the write-up since beneficial as i would. Yet again many thanks.

  4. Are the issues really as complex as they seem?

  5. I don’t know if it’s just me or if everybody else experiencing issues with your site. It appears as though some of the written text on your content are running off the screen. Can someone else please provide feedback and let me know if this is happening to them as well? This could be a problem with my web browser because I’ve had this happen before. Appreciate it

  6. Thanks for discussing the issues and covering them in a well written format.

  7. What made you first develop an interest in this topic?

  8. Hi there, just became aware of your blog through Google, and found that it’s truly informative. It’s important to cover these trends.

  9. Most often since i look for a blog Document realize that the vast majority of blog pages happen to be amateurish. Not so,We can honestly claim for which you writen is definitely great and then your webpage rock solid.

  10. Well, I don’t know if that’s going to work for me, but definitely worked for you! 🙂 Excellent post!

  11. Only a smiling visitor here to share the love (:, btw outstanding style and design .

  12. lgrpjijj表示:

    hydroxychloroquine 200mg order plaquenil for sale order plaquenil pill

  13. PSM1 exam表示:

    Wish I’d thought of this. Am in the field, but I procrastinate alot and haven’t written as much as I’d like. Thanks.

  14. ukduhyxx表示:

    is erythromycin a sulfa drug erythromycin ophthalmic ointment usp

  15. I really like your writing style, excellent info , thanks for putting up : D.

  16. jackelee表示:

    No other software satisfies all of the user’s needs as easily.
    Hide Computer can be found in the zipped app’s downloading page:
    (c) 2014-2019 Disk DrillHomestead by Google+

    Menu

    Dining Around Scrapbooking

    I like to try new restaurants and I love the fascination of googling what restaurants are new or up and coming https://www.almawomenboutique.com/wp-content/uploads/2022/06/mikenr.pdf
    50e0806aeb jackelee

  17. giozyko表示:

    I am a independent Seeking Alpha stylist. I work with large pre-IPO businesses offering early asset appreciation, and vice-versa.

    Tuesday, March 02, 2008

    The Quotes

    The dynamic between Road Runner Express (RAE) and Coyote is one of the more interesting ones out there, as they are both California based companies, with RAE being quite large with vast tracks. RAE has often been described as the “Howard Hughes/Lath https://guapolocotattoo.it/wp-content/uploads/2022/06/123_Bulk_Email_Direct_Sender.pdf
    50e0806aeb giozyko

  18. gersas表示:

    Advantages:
    ■ UniWebsite is easy to use, yet full featured
    ■ After registering your website, you can edit content offline. When you want to publish the website you are taken to the Registration page and asked to register the website.
    ■ You can publish your website if you have a working Internet connection and a hosted website space. However, you cannot publish your website if you do not have these two items.
    ■ Content updates can https://xn--wo-6ja.com/upload/files/2022/06/xOavcEPgTfnNRpzWtsCz_06_d49bb0091bbebc1854b11d2900157fd7_file.pdf
    50e0806aeb gersas

  19. denzfrod表示:

    –help = list options or displays help information
    –version = display version and copyright information (NOTE:
    Cinmidi was created by Gordon Armstrong, 20 August 1999)
    –help – this message
    Don’t display MIDI input data.
    Usage: cinmidi [-a][-i][-x-d-2] [infile]
    Options:
    -a = display MIDI timing in absolute time instead of delta time
    -i = don’t display interpretation of MIDI message as comments
    -d = display MIDI bytes in decimal form
    -x = display MIDI bytes in hexadecimal form https://siddhaastrology.com/wp-content/uploads/2022/06/savgreg.pdf
    50e0806aeb denzfrod

發佈留言

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