
- AngularJS 教程
- AngularJS - 首頁
- AngularJS - 概述
- AngularJS - 環境搭建
- AngularJS - MVC 架構
- AngularJS - 第一個應用
- AngularJS - 指令
- AngularJS - 表示式
- AngularJS - 控制器
- AngularJS - 過濾器
- AngularJS - 表格
- AngularJS - HTML DOM
- AngularJS - 模組
- AngularJS - 表單
- AngularJS - 包含
- AngularJS - AJAX
- AngularJS - 檢視
- AngularJS - 作用域
- AngularJS - 服務
- AngularJS - 依賴注入
- AngularJS - 自定義指令
- AngularJS - 國際化
- AngularJS 應用
- AngularJS - 待辦事項應用
- AngularJS - 記事本應用
- AngularJS - Bootstrap 應用
- AngularJS - 登入應用
- AngularJS - 上傳檔案
- AngularJS - 內聯應用
- AngularJS - 導航選單
- AngularJS - 切換選單
- AngularJS - 訂單表單
- AngularJS - 搜尋選項卡
- AngularJS - 拖拽應用
- AngularJS - 購物車應用
- AngularJS - 多語言應用
- AngularJS - 圖表應用
- AngularJS - 地圖應用
- AngularJS - 分享應用
- AngularJS - 天氣應用
- AngularJS - 計時器應用
- AngularJS - Leaflet 應用
- AngularJS - Lastfm 應用
- AngularJS 有用資源
- AngularJS - 問答
- AngularJS - 快速指南
- AngularJS - 有用資源
- AngularJS - 討論
AngularJS - 控制器
AngularJS 應用主要依賴控制器來控制應用程式中的資料流。控制器使用 `ng-controller` 指令定義。控制器是一個包含屬性/特性和函式的 JavaScript 物件。每個控制器都接受 `$scope` 作為引數,它指的是控制器需要處理的應用程式/模組。
<div ng-app = "" ng-controller = "studentController"> ... </div>
這裡,我們使用 ng-controller 指令宣告一個名為 `studentController` 的控制器。我們定義如下:
<script> function studentController($scope) { $scope.student = { firstName: "Mahesh", lastName: "Parashar", fullName: function() { var studentObject; studentObject = $scope.student; return studentObject.firstName + " " + studentObject.lastName; } }; } </script>
`studentController` 定義為一個以 `$scope` 作為引數的 JavaScript 物件。
`$scope` 指的是使用 `studentController` 物件的應用程式。
`$scope.student` 是 `studentController` 物件的一個屬性。
`firstName` 和 `lastName` 是 `$scope.student` 物件的兩個屬性。我們為它們傳遞預設值。
`fullName` 屬性是 `$scope.student` 物件的一個函式,它返回組合後的姓名。
在 `fullName` 函式中,我們獲取 `student` 物件,然後返回組合後的姓名。
需要注意的是,我們也可以在單獨的 JS 檔案中定義控制器物件,並在 HTML 頁面中引用該檔案。
現在我們可以使用 `ng-model` 或表示式來使用 `studentController` 的 `student` 屬性,如下所示:
Enter first name: <input type = "text" ng-model = "student.firstName"><br> Enter last name: <input type = "text" ng-model = "student.lastName"><br> <br> You are entering: {{student.fullName()}}
我們將 `student.firstName` 和 `student.lastName` 繫結到兩個輸入框。
我們將 `student.fullName()` 繫結到 HTML。
現在,每當您在名字和姓氏輸入框中輸入任何內容時,您都可以看到全名會自動更新。
示例
以下示例顯示了控制器的用法:
testAngularJS.htm
<html> <head> <title>Angular JS Controller</title> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script> </head> <body> <h2>AngularJS Sample Application</h2> <div ng-app = "mainApp" ng-controller = "studentController"> Enter first name: <input type = "text" ng-model = "student.firstName"><br> <br> Enter last name: <input type = "text" ng-model = "student.lastName"><br> <br> You are entering: {{student.fullName()}} </div> <script> var mainApp = angular.module("mainApp", []); mainApp.controller('studentController', function($scope) { $scope.student = { firstName: "Mahesh", lastName: "Parashar", fullName: function() { var studentObject; studentObject = $scope.student; return studentObject.firstName + " " + studentObject.lastName; } }; }); </script> </body> </html>
輸出
在 Web 瀏覽器中開啟 `testAngularJS.htm` 檔案並檢視結果。