AngularJS 控制器
AngularJS 控制器 控制 AngularJS 應(yīng)用程序的數(shù)據(jù)。
AngularJS 控制器是常規(guī)的 JavaScript 對(duì)象。
AngularJS 控制器
AngularJS 應(yīng)用程序被控制器控制。
ng-controller 指令定義了應(yīng)用程序控制器。
控制器是 JavaScript 對(duì)象,由標(biāo)準(zhǔn)的 JavaScript 對(duì)象的構(gòu)造函數(shù) 創(chuàng)建。
AngularJS 實(shí)例
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><div ng-app="myApp" ng-controller="personCtrl">名: <input type="text" ng-model="firstName"><br>姓: <input type="text" ng-model="lastName"><br><br>姓名: {{fullName()}}</div><script>var app = angular.module('myApp', []);app.controller('personCtrl', function($scope) { $scope.firstName = "John"; $scope.lastName = "Doe"; $scope.fullName = function() { return $scope.firstName + " " + $scope.lastName; }});</script></body></html>
運(yùn)行結(jié)果:
名:
姓:
姓名: John Doe
AngularJS 應(yīng)用程序由 ng-app 定義。應(yīng)用程序在 <div> 內(nèi)運(yùn)行。
ng-controller="myCtrl" 屬性是一個(gè) AngularJS 指令。用于定義一個(gè)控制器。
myCtrl 函數(shù)是一個(gè) JavaScript 函數(shù)。
AngularJS 使用$scope 對(duì)象來調(diào)用控制器。
在 AngularJS 中, $scope 是一個(gè)應(yīng)用象(屬于應(yīng)用變量和函數(shù))。
控制器的 $scope (相當(dāng)于作用域、控制范圍)用來保存AngularJS Model(模型)的對(duì)象。
控制器在作用域中創(chuàng)建了兩個(gè)屬性 (firstName 和 lastName)。
ng-model 指令綁定輸入域到控制器的屬性(firstName 和 lastName)。
控制器方法
上面的實(shí)例演示了一個(gè)帶有 lastName 和 firstName 這兩個(gè)屬性的控制器對(duì)象。
控制器也可以有方法(變量和函數(shù)):
AngularJS 實(shí)例
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><div ng-app="myApp" ng-controller="personCtrl">名: <input type="text" ng-model="firstName"><br>姓: <input type="text" ng-model="lastName"><br><br>姓名: {{fullName()}}</div><script>var app = angular.module('myApp', []);app.controller('personCtrl', function($scope) { $scope.firstName = "John"; $scope.lastName = "Doe"; $scope.fullName = function() { return $scope.firstName + " " + $scope.lastName; }});</script></body></html>
運(yùn)行效果:
名:
姓:
姓名: John Doe
外部文件中的控制器
在大型的應(yīng)用程序中,通常是把控制器存儲(chǔ)在外部文件中。
只需要把 <script> 標(biāo)簽中的代碼復(fù)制到名為 personController.js 的外部文件中即可:
AngularJS 實(shí)例
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><div ng-app="myApp" ng-controller="personCtrl">名: <input type="text" ng-model="firstName"><br>姓: <input type="text" ng-model="lastName"><br><br>姓名: {{firstName + " " + lastName}}</div><script src="personController.js"></script></body></html>
運(yùn)行結(jié)果:
名:
姓:
姓名: John Doe
其他實(shí)例
以下實(shí)例創(chuàng)建一個(gè)新的控制器文件:
angular.module('myApp', []).controller('namesCtrl', function($scope) { $scope.names = [ {name:'Jani',country:'Norway'}, {name:'Hege',country:'Sweden'}, {name:'Kai',country:'Denmark'} ];});
保存文件為 namesController.js:
然后,在應(yīng)用中使用控制器文件:
AngularJS 實(shí)例
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><div ng-app="myApp" ng-controller="namesCtrl"> <ul> <li ng-repeat="x in names"> {{ x.name + ', ' + x.country }} </li></ul></div><script src="namesController.js"></script></body></html>
運(yùn)行效果:
以上就是對(duì)AngularJS 控制器資料的整理,后續(xù)繼續(xù)補(bǔ)充。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注