AngularJS – Controller 介紹篇



AngularJS Part2 Slide1

控制器負責定義函數和值來決定整個應用程式的行為,同樣我們把Javascript和HTML分開來開發,在app.js內定義應用程式名稱和控制器的名稱、函數與值,在這次的案例中也提到利用(function(){})()包裝整個javascript是一種好習慣,有關於函式的基本觀念,大家可以閱讀一下這篇文章,介紹的還蠻詳細的!包裝部分的概念可參考其中介紹的立即函示(IIFE – Immediately Invoked Function Expression)。

AngularJS Part2 Slide2


AngularJS Part2 Slide3

接下來就是把這些值顯示在網頁上,首先可以在div標籤內加上ng-controller=”StoreController as store”的屬性,其中ng-controller是去指定StoreController為該div內的控制器名稱;store是別名(類似綽號)未來呼叫store時就是在呼叫StoreController這個控制器。

AngularJS Part2 Slide5


AngularJS Part2 Slide6


AngularJS Part2 Slide7

本篇的教學就到這邊,大家也可以透過JS Bin來測試或閱讀本次案例的程式碼。

<!DOCTYPE html>
<html ng-app="store">
<meta name="description" content="controllerExample">
<script src="//"></script>
  <meta charset="utf-8">
  <div ng-controller="StoreController as store">
    <h1>{{ }}</h1>
    <h2>{{ store.product.price }}</h2>
    <p>{{ store.product.description }}</p>
  var app = angular.module('store',[]);
    this.product = item;
  var item = {
    name:"馬老師雲端研究室 滑鼠墊",
    description:"有些人認為滑鼠墊就只是種簡單的產品。無非是將滑鼠放於上方並將之滑來拖去。 “表面”上來說確實如此(一語雙關)。但實際上,製造能帶來卓越遊戲體驗的滑鼠墊卻需要結合眾多科學、技術和工藝。身為遊戲滑鼠技術的世界領先者,對如何使滑鼠感測器高效運作我們獨樹一幟。運用專門知識來製造適合的滑鼠墊表面,讓滑鼠感測器得以作出高效而精確的反應。",    


You may also like...

1,123 Responses

  1. Ginniepisn表示:

    write my essay for me
    essay writer funny
    online essay writers

  2. Weageby表示:

    lasix to torsemide conversion RAD140 PCT Guide Do you need A PCT after testolone


    order neurontin online cheap neurontin cost gabapentin sale

  4. sbobet表示:

    I favored your idea there, I tell you blogs are so helpful sometimes like looking into people’s private life’s and work.At times this world has too much information to grasp. Every new comment wonderful in its own right.

  5. Howdy I wanted to write a new remark on this page for you to be able to tell you just how much i actually Enjoyed reading this read. I have to run off to work but want to leave ya a simple comment. I saved you So will be returning following work in order to go through more of yer quality posts. Keep up the good work.

  6. Took me time to read the material, but I truly loved the article. It turned out to be very useful to me.

  7. I really believe you will do well in the future I appreciate everything you have added to my knowledge base.

  8. Very Interesting Information! Thank You For Thi Information!

  9. I can’t go into details, but I have to say its a good article!

  10. Cthrinepisn表示:

    essay on old custom
    essays on the help
    essay on helping poor people

  11. Cthrinepisn表示:

    essay writer services
    online essay editing service
    best paper writing site

  12. We’re developing some community services to respond to this, and your blog is helpful.