AngularJS應用主要依賴于控制器來控制數據在應用程序中的流動。控制器采用ng-controller指令定義。控制器是一個包含屬性/屬性和JavaScript對象的功能。每個控制器接受$scope參數指定應用程序/模塊,由控制器控制。
<div ng-app="" ng-controller="studentController">...</div>
在這里,我們已經聲明采用ng-controller指令的控制器studentController。作為下一步,我們將定義studentController如下
<script>function studentController($scope) {  $scope.student = {   firstName: "yiibai",   lastName: "com",   fullName: function() {     var studentObject;     studentObject = $scope.student;     return studentObject.firstName + " " + studentObject.lastName;   }  };}</script>  現在可以使用ng-model或使用表達式如下使用studentController學生的屬性。
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()}}  例子
下面的例子將展示使用控制器。
testAngularJS.html 文件內容如下:
<html><head><title>Angular JS Controller</title></head><body><h2>AngularJS Sample Application</h2><div ng-app="" 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>function studentController($scope) {  $scope.student = {   firstName: "Mahesh",   lastName: "Parashar",   fullName: function() {     var studentObject;     studentObject = $scope.student;     return studentObject.firstName + " " + studentObject.lastName;   }  };}</script><script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script></body></html>輸出
在Web瀏覽器打開textAngularJS.html,看到以下結果:

新聞熱點
疑難解答