Your posts match “ Controller ” tag:

AngularJS - Controller亂亂玩

其實我就只是隨便寫了一個小東西,順便練習所謂的ng-repeat,發現到其實很容易使用。

AngularJS是一個MVC framework,而在AngularJS中可以透過ng-controller這個屬性來指定要採用的Controller,所以直接來看code吧,或是在JSBin上閱讀也可以。

HTML

<!DOCTYPE html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body ng-controller="PhonesController">
  <ul>
    <li ng-repeat="phone in phones">
      <span class="name">{{phone.name}}</span> made by <span class="brand">{{phone.brand}}</span>
    </li>
  </ul>
</body>
</html>

Controller

function PhonesController($scope){
  $scope.phones = [
    {name: "iPhone",     brand: "Apple"},
    {name: "iPhone 3G",  brand: "Apple"},
    {name: "iPhone 3Gs", brand: "Apple"},
    {name: "iPhone 4",   brand: "Apple"},
    {name: "iPhone 4s",  brand: "Apple"},
    {name: "iPhone 5",   brand: "Apple"},
  ];
  
}

我們在<body></body>中宣告了採用PhonesController這個Controller,然後AngularJS會把整個body給當做是一個scope,這樣一來這個scope就會跟$scope這個變數有關係。

然後我們在這個scope上綁了一個叫做phones的變數,然後在HTML中我們用很神奇的ng-repeat這個參數來進行iterator的行為,把它給render到前端去 ...

至於,一個HTML文件內可以有多少個Controller呢?這個我還要再測一下就是了 ...