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

3,320 Responses

  1. walbir表示:

    German submarine U-320

    German submarine U-320 was a Type VIIC U-boat of Nazi Germany’s Kriegsmarine during World War II. The submarine was laid down on 11 September 1941 at the DeSchiMAG AG Weser yard at Bremen, launched on 15 February 1942 and commissioned on 11 April 1942 under the command of Oberleutnant zur See Christian Ehrhardt-Nortkamp.

    Design
    German Type VIIC submarines were preceded http://www.flexcompany.com.br/flexbook/upload/files/2022/06/VEnOcEzatkXhcPE1dXbp_06_2dbc8d2e148019eab28a222fdf24be0d_file.pdf
    50e0806aeb walbir

  2. junvoln表示:

    Also, it contains a plugins manager, with the plugins list in accessiblity or otherwise fully customizable, allowing you to add the ones you want on your dock and still be able to use it when you know how to operate an administrative console.
    It also contains a unified “very similar” properties menu to the one on Mac OS X, allowing you to easily transfer any change you make to this new dock.

    System requirements:- Windows Vista, Windows XP (with SP1), Windows https://popstay.info/upload/files/2022/06/deDBG4oiQZcMSStlLxcV_06_d48ea701ebf8ca7407d0e48607437536_file.pdf
    50e0806aeb junvoln

  3. evgegar表示:

    With SmartWatcher, the service does not need access to your passwords or any API keys.
    The code sample below is creating an instance of SmartWatcher with the builder pattern:
    SmartWatcher watcher = new SmartWatcherBuilder()
    .withSubDomains(“abc.com”)
    .withRegions(“WakeupCountry”)
    .withNot https://csvcoll.org/portal/checklists/checklist.php?clid=10648
    50e0806aeb evgegar

  4. worfynb表示:

    eSAT is the Video Broadcasting Center.
    Today videos, movies, songs…present themselves on the Internet, but there are missing in continuity, in originality, in set, in post-production…in other words, there is a huge gap between what people see and what it is.
    eSAT solves all problems, content, technical, legal and that can be…And more »

    MP3 Download v1.0.5(Feb 16, 2009) https://sattology.org/wp-content/uploads/2022/06/Qwant_for_Chrome.pdf
    50e0806aeb worfynb

  5. karkarl表示:

    We have tested OkayKeybees for months now and found it an agreeable solution that allows us to configure our keyboard layout in a straightforward way.

    Installation

    The installation process is straightforward and doesn’t require any type of third-party software. Simply download the software for free from Okaykeybees’ website and it will copy on any USB flash drive or other storage media. This allows you to boot the computer from another partition that you can insert the removable media on. https://cililojemic.wixsite.com/ficlrecarlla/post/jpropsedit-crack-download-x64
    50e0806aeb karkarl

  6. raemmahk表示:

    ACDSee Photo Manager is the software package for any digital photo enthusiast. It comes with features to create slide shows, organize albums, search through pictures and add keywords, and more.
    In addition to that, ACDSee Photo Manager allows you to align photos by date, keywords or location, apply filters, retouch them and adjust contrast, brightness and saturation. The program also supports slideshow makers, includes some extra tools for users who want to scrapbook and create greeting cards, receives https://allweneedistrance.com/wp-content/uploads/2022/06/beniham.pdf
    50e0806aeb raemmahk

  7. cdttodnl表示:

    erythromycin medication erythromycin vs tetracycline

  8. samtri表示:

    var validator = require(‘../’);
    var test = require(‘tape’);
    var parse = validator.parse;
    var object = parse([‘a[attr]’, ‘a[attr=”val”]’]);

    test(‘nested’, function (t) {
    t.deepEqual(object, [
    { ‘a’: { ‘attr’: ‘val’ } }
    ]); https://travelfamilynetwork.com/wp-content/uploads/2022/06/hellgill.pdf
    ec5d62056f samtri

  9. yemsyl表示:

    Some of the software used in this review are also published here:
    Adobe Acrobat Pro DC
    Adobe Lightroom
    Adobe Photoshop CS6
    Adobe Audition
    Adobe Premiere Pro
    Adobe Media Encoder
    Adobe Encore
    ConvertXtoDVD
    Cooliris (for Black Screen Problem)
    Dell Laptop: Dell Inspiron 15 7000 https://sokhanedoost.com/wp-content/uploads/2022/06/nakikel.pdf
    ec5d62056f yemsyl

  10. veetdilw表示:

    Check out the worksheets!
    Chunk is a particularly rich worksheet which gives complete information on much of the process, with constraints, progress reports, hyper-links to component bodies, and even a high-level widget listing defining constrained body forms.
    Of course CamSketchpad is free – no pledge or requested donation allowed.
    CamSketchpad can be run locally on Mac OS X or Linux, on a server (at speeds of over 200 request/sec) https://gobigup.com/wp-content/uploads/2022/06/ecstan.pdf
    ec5d62056f veetdilw

  11. nabyord表示:

    Features

    High grade componentization

    Data driven Entities and Fields

    A comprehensive testable entity model.

    Object oriented

    It’s fully object oriented.
    That means you can change anything you want.

    Testing

    It contains over 650 functional tests, written in Python.
    That gives you a good test coverage for the tool.

    A comprehensive test suite for robustness

    Open Source

    Antix SMTP Impostor is https://hundhatohurofigiso.wixsite.com/cepbeavi/post/zellochannelshacks-exclusive
    ec5d62056f nabyord

  12. baicha表示:

    C. John McMillan author C. John McMillan has been coding since he programmed his school’s yearbook system on a TRS-80 Model I. He started learning Windows programming in the late ’90s. He works as a software engineer at Buffer, which is a startup company that enables people to share and manage video content on the Web. Prior to joining Buffer, John worked as a Windows software developer at…

    EaseUS Todo Backup is designed to do http://hotelthequeen.it/wp-content/uploads/2022/06/yargeni.pdf
    ec5d62056f baicha

  13. ferniq表示:

    How useful was this post? Click on a star to rate it!

    Submit Rating Average rating 0 / 5. Vote count: 0 No votes so far! Be the first to rate this post. We are sorry that this post was not useful for you! Let us improve this post! Tell us how we can improve this post? Submit Feedback

    Share this: Facebook

    Twitter

    Reddit

    LinkedIn

    Tumblr

    Q:

    Iphone https://rnxtv.com/wp-content/uploads/2022/06/jessjay.pdf
    ec5d62056f ferniq

  14. quarhes表示:

    This application is compatible with Windows XP/Vista/7/8/10.

    VLC Media Player 2019-09-06 ВМ VLC has long been a popular choice for video playback, because it’s highly configurable and reliable.

    A lightweight media player with a simple interface and a classic user…

    1.1.3390

    2016-04-26

    – Added closed caption encoding capabilities via the CMSS-3 encoder https://fullrangemfb.com/icc-immo-code-calculator-v1-5-3/
    ec5d62056f quarhes

  15. darodawn表示:

    The N-terminal region of the presenilin 1 protein is essential for cyclophilin A cleavage in gamma-secretase activity assays.
    Presenilin 1 (PS1) is a major component of the gamma-secretase complex that cleaves the beta-amyloid precursor protein to generate the amyloid-beta peptide. PS1 has several distinct regions that appear to serve different functional roles within gamma-secretase. In this study, https://hidden-tundra-52591.herokuapp.com/state_property_chain_gang_vol_2_zip.pdf
    ec5d62056f darodawn

  16. brelau表示:

    The shim functions as a user-mode hook, which means that it intercepts all Win32 API functions. A shim might run the application (for example, to collect data), complete the Win32 API function calls, or wrap the end of the Win32 API function calls with a safe function call. When a shim completes an API function call, it passes the context of the function call (for example, the input parameters and the return values of the function call) to the https://worldwidefellowship.org/wp-content/uploads/2022/06/mairag.pdf
    ec5d62056f brelau

  17. trijare表示:

    �■ Only for images which are already digitalized (This application doesn’t work on scanned films)
    �■ The best result has been obtained on images in which the film grain is high
    �■ Black is not smoothed.
    �■ Only one image can be opened at a time in this application.
    �■ Compatibility:
    * Linux
    * Windows
    * Mac OS X
    The program is written in C++ and so https://solvefortomorrow.com/upload/files/2022/06/GUWdKkz6G3uDyN7hhaFZ_04_3e202bd1ad726a3deec4e4ef42ea943e_file.pdf
    ec5d62056f trijare

  18. bredeae表示:

    The clever thing about LightShow is that you can change the focus of the stage to individual lights, creating an interesting effect.
    DMXOut does the same function set as LightShow and LightShow has been expanded to include entire rooms and more.
    ]]>
    Fri, 02 Oct 2012 14:50:08 +0000

    If theres something I http://s3.amazonaws.com/videoondemand-source-8vbop216albp/wp-content/uploads/2022/06/04185823/olwepla.pdf
    ec5d62056f bredeae

  19. allagon表示:

    If you want to change the speed setting and want to save it, you may use the mousewheel from that popup box to scroll down and then doubleclick the scrollbar, so that the speed setting will be set to the current mousewheel value. The speed can be altered by holding shift key and then moving the mousewheel back and forward.

    The tool is completly unintegrable in Linux, but as soon as possible I will make an deb-package for it. http://powervapes.net/zen-garden-pro-editor-v1-3-rar/
    ec5d62056f allagon

  20. rehatom表示:

    However, Quick Screen Capture Tool is a convenient tool to create screenshots. It is useful for capturing, saving, and uploading screenshots and, as such, you should try it out if you’re in need of taking screenshots.
    Yes, you got it right, as a matter of fact, it’s screen capture tool. It is a tool that provides you with abilities to capture part of your desktop screen so that you can use it to create screenshots.
    Now it is possible https://ebbsarrivals.com/2022/06/04/kuncijawabanbukupripsterpadukelas8intanpariwara-best/
    ec5d62056f rehatom

發佈留言

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