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

  1. edwajan表示:

    The program can save drawing views and print them to a printer. The program also supports several fonts, including the original drawing, companion books and your own modified fonts.

    UIBoardPro is an award-winning application for creating PCBs. It excels at creating tiled boards of complex shape where every tile is an individual component, and subassemblies are created by jointing tiles.
    You can create multilayer boards, by depicting alternate signal and power layers between tiles. If https://www.waspsnetball.co.uk/profile/ycaninviztuara/profile
    66cf4387b8 edwajan

  2. kamsafe表示:

    Braitenberg Vehicle Simulator is written in Java and is a Java-based application. Nothing special is required other than your java runtime environment, which is located in your application’s directory. For Windows, Java Runtime Environment can be obtained from Unfortunately, Java was not included in the package.

    The setup program extracts Java Runtime Environment from your Internet Explorer, then adds the Java Runtime Environment to your computer. https://www.deadhorsebranding.com/profile/Meu-Adoravel-Androide-1-E-2-Download-Fix-Dublado-Pt-Br/profile
    66cf4387b8 kamsafe

  3. chederr表示:

    And in all cases, all audio and video files will be converted to be Mp3 format without change the original data format of files.
    Detailed Features
    Super Audio Factory Features:
    *Auto Batch Converting
    Super Audio Factory can convert hundreds of files at a time if you have enough memory.
    Simple: With just two buttons, Super Audio Factory is easy and non-intimidating to anyone.
    Compressed: Super Audio Factory can significantly reduce output size while using the https://www.pianolessonsinyourhome.com/profile/Ushtrime-Te-Zgjidhura-Matematika-E-Avancuar-10-Pegizip/profile
    66cf4387b8 chederr

  4. nevifyn表示:

    The properties of an associated system that make it suitable for use in the field of the invention will be readily understood by referring to two preferred forms of the system and these use a patentability demonstration weight loss system enabling a person to monitor success and significance of weight loss. In fact, such a system may consist of a single device as described herein or it may comprise one or more devices that are associated with one another to act in an aggregate fashion to aid in weight loss.
    It is often difficult https://www.pyramidvalley.org/profile/Long-Range-Shooting-Simulation-Iii-Full-Download/profile
    66cf4387b8 nevifyn

  5. callchav表示:

    Fortunately, the provided direct installation method requires no particular effort, so it’s easy to get it on your PC for good.
    Uninstaller
    10
    Free
    Release date: December 7, 2017
    Price: Free
    Download size: 2.66 MB
    System requirements
    OS: Windows 7 and newer
    Media type: Text-based and WAV
    QueuePlay
    TECHNICAL PRICE Free
    Some of the scenario details from other Players
    REAL TIME ( https://www.entrepreneurly.in/profile/AutoCAD/profile
    99d5d0dfd0 callchav

  6. AndrewEcork表示:

    cialis coupon cialis pills generis cialis

  7. AndrewEcork表示:

    sell of cialis best price for daily cialis online cialis canada

  8. AndrewEcork表示:

    cialis daily pricing buy tadalafil is generic cialis safe

  9. greasafo表示:

    UpDown
    bd86983c93 greasafo

  10. top表示:

    You write Formidable articles, keep up good work.

  11. hot表示:

    I think this is among the so much vital info for me. And i’m happy reading your article. But wanna remark on few common issues, The site style is wonderful, the articles is really excellent : D. Just right job, cheers

  12. tube表示:

    If wings are your thing, Tinker Bell’s sexy Halloween costume design is all grown up.

發佈留言

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