AngularJS控制器詳解及示例代碼
AngularJS應(yīng)用主要依賴于控制器來控制數(shù)據(jù)在應(yīng)用程序中的流動??刂破鞑捎胣g-controller指令定義??刂破魇且粋€包含屬性/屬性和JavaScript對象的功能。每個控制器接受$scope參數(shù)指定應(yīng)用程序/模塊,由控制器控制。
<div ng-app="" ng-controller="studentController"> ... </div>
在這里,我們已經(jīng)聲明采用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>
studentController 定義 $scope 作為JavaScript對象參數(shù)。
$scope 表示應(yīng)用程序,使用studentController對象。
$scope.student 是studentController對象的屬性。
firstName和lastName是$scope.student 對象的兩個屬性。我們已經(jīng)通過了默認值給他們。
fullName 是$scope.student對象的函數(shù),它的任務(wù)是返回合并的名稱。
在fullName函數(shù)中,我們現(xiàn)在要學(xué)生對象返回組合的名字。
作為一個說明,還可以定義控制器對象在單獨的JS文件,并把有關(guān)文件中的HTML頁面。
現(xiàn)在可以使用ng-model或使用表達式如下使用studentController學(xué)生的屬性。
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()}}
現(xiàn)在有 student.firstName 和 student.lastname 兩個輸入框。
現(xiàn)在有 student.fullName()方法添加到HTML。
現(xiàn)在,只要輸入first name和lastname輸入框中輸入什么,可以看到兩個名稱自動更新。
例子
下面的例子將展示使用控制器。
testAngularJS.html 文件內(nèi)容如下:
<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,看到以下結(jié)果:

以上就是AngularJS控制器的資料整理,后續(xù)繼續(xù)整理相關(guān)知識,謝謝大家對本站的支持。
相關(guān)文章
Angular使用過濾器uppercase/lowercase實現(xiàn)字母大小寫轉(zhuǎn)換功能示例
這篇文章主要介紹了Angular使用過濾器uppercase/lowercase實現(xiàn)字母大小寫轉(zhuǎn)換功能,涉及AngularJS過濾器針對字符串轉(zhuǎn)換的簡單使用技巧,需要的朋友可以參考下2018-03-03
在 Angular 中實現(xiàn)搜索關(guān)鍵字高亮示例
本篇文章主要介紹了在 Angular 中實現(xiàn)搜索關(guān)鍵字高亮示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
AngularJS 與Bootstrap實現(xiàn)表格分頁實例代碼
這篇文章主要介紹了AngularJS 與Bootstrap實現(xiàn)表格分頁的相關(guān)資料,并附實例代碼和實現(xiàn)效果圖,需要的朋友可以參考下2016-10-10
快速解決angularJS中用post方法時后臺拿不到值的問題
今天小編就為大家分享一篇快速解決angularJS中用post方法時后臺拿不到值的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
AngualrJS中每次$http請求時的一個遮罩層Directive
AngularJS是一款非常強大的前端MVC框架。接下來通過本文給大家介紹AngualrJS中每次$http請求時的一個遮罩層Directive,本文非常具有參考借鑒價值,特此分享供大家學(xué)習(xí)2016-01-01
Angular入口組件(entry component)與聲明式組件的區(qū)別詳解
這篇文章主要給大家介紹了關(guān)于Angular入口組件(entry component)與聲明式組件的區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04

