简述AngularJS的控制器的使用
2015-06-16 12:10
633 查看
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,看到以下结果:
您可能感兴趣的文章:
相关文章推荐
- ASP.NET Web API教程 创建Admin控制器实例分享
- C# 如何在MVC3中取消备用控制器的选择
- angularJS 中$attrs方法使用指南
- 创建你的第一个AngularJS应用的方法
- AngularJS语法详解
- angularjs中的e2e测试实例
- ThinkPHP控制器里javascript代码不能执行的解决方法
- AngularJS + Node.js + MongoDB开发的基于高德地图位置的通讯录
- thinkphp控制器调度使用示例
- ThinkPHP中URL路径访问与模块控制器之间的关系
- AngularJS的内置过滤器详解
- ThinkPHP3.2.2的插件控制器功能简述
- Laravel 5框架学习之路由、控制器和视图简介
- Laravel 5框架学习之模型、控制器、视图基础流程
- ASP.NET MVC 控制器与视图
- angularJS结合canvas画图例子
- 教你用AngularJS框架一行JS代码实现控件验证效果
- AngularJS入门教程(零):引导程序
- AngularJS基础学习笔记之简单介绍
- AngularJS学习笔记之ng-options指令