- 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 - ToDo 应用程序
- AngularJS - 记事本应用程序
- AngularJS - 引导应用程序
- AngularJS - 登录应用程序
- AngularJS - 上传文件
- AngularJS - 内嵌应用程序
- AngularJS - 导航菜单
- AngularJS - 切换菜单
- AngularJS - 订单
- AngularJS - 搜索选项卡
- AngularJS - 拖动应用程序
- AngularJS - 购物车应用程序
- AngularJS - 翻译应用程序
- AngularJS - 图表应用
- AngularJS - 地图应用程序
- AngularJS - 共享应用程序
- AngularJS - 天气应用
- AngularJS - 计时器应用程序
- AngularJS - 传单应用程序
- 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 被定义为一个 JavaScript 对象,并以 $scope 作为参数。
$scope 指的是使用studentController 对象的应用程序。
$scope.student 是studentController 对象的一个属性。
FirstName 和 LastName 是 $scope.student 对象的两个属性。我们将默认值传递给他们。
属性 fullName 是 $scope.student 对象的函数,它返回组合名称。
在 fullName 函数中,我们获取学生对象,然后返回组合名称。
请注意,我们还可以在单独的 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并查看结果。
