实现视图示例

Earlier we looked at Angular Directives and its usage. Now we will shift our focus to the Model View Controller (MVC) part of AngularJS. We will discuss about the Controller, View and the Scope (view model) in this tutorial.

前面我们介绍了Angular Directives及其用法。 现在,我们将重点转移到AngularJS的模型视图控制器(MVC)部分。 在本教程中,我们将讨论Controller,View和Scope(视图模型)。

内容 (Contents)

  • Controller, Scope and View Concepts 控制器,范围和视图概念
  • Guidelines指导方针
  • Controller and Scope Example控制器和示波器示例

控制器,范围和视图概念 (Controller, Scope and View Concepts)

Controllers are JavaScript objects that controls the view data of angular applications. In the previous post, we looked at some of the Angular directives and the data binding concepts. We use controllers to control the data that are bound to the View and Scope acts as glue between the View and Controller.

控制器是JavaScript对象,用于控制角度应用程序的视图数据。 在上一篇文章中 ,我们介绍了一些Angular指令和数据绑定概念。 我们使用控制器来控制绑定到View的数据,并且Scope充当ViewController之间的粘合剂

The directive ng-controller is used to attach the application controller to the DOM and angular will instantiate a new controller object and a new child scope will be available to the constructor of the controller. The new child scope can be injected into constructor as $scope.

指令ng-controller用于将应用程序控制器附加到DOM,而angular将实例化一个新的控制器对象,并且新的子作用域可用于该控制器的构造函数。 新的子作用域可以作为$scope.注入到构造函数中$scope.

Scope is an execution context for the angular expressions and it can watch expressions and propagate events. Scopes also provides APIs to watch the changes in the model and view. Scope ties the view to the controller so that it does not need to know the view and the view does not need to know about the controller.

范围是角度表达式的执行上下文,它可以监视表达式并传播事件。 Scopes还提供API来监视模型视图中的更改。 范围将视图与控制器绑定在一起,因此它不需要知道该视图并且该视图也不需要知道控制器。

指导方针 (Guidelines)

AngularJS official documentation provides the following guidelines to use the controllers in the angular applications.

AngularJS官方文档提供了以下准则,以便在angular应用程序中使用控制器。

  • Use controllers to set up the initial state of the $scope object and add behavior to that object.使用控制器来设置$ scope对象的初始状态并将行为添加到该对象。
  • Do not use controllers to manipulate DOM. It should contain only business logic. Use Data Binding and Directives for DOM manipulation.不要使用控制器来操作DOM。 它应仅包含业务逻辑。 使用数据绑定指令进行DOM操作。
  • Do not use controllers to format inputs – Use Angular Form controls.不要使用控制器来格式化输入–使用Angular Form控件。
  • Do not use to filter output – Use Angular Filters.请勿用于过滤输出–使用角度过滤器。
  • Do not use controllers to share code or state across controllers – Use Angular Services.不要使用控制器在控制器之间共享代码或状态–使用Angular Services。
  • Do not manage the life cycle of other components using controllers.不要使用控制器来管理其他组件的生命周期。

We will see features like Angular Form controls, Filters and Services in the upcoming tutorials.

在接下来的教程中,我们将看到诸如Angular Form控件过滤器服务之类的功能。

控制器和示波器示例 (Controller and Scope Examples)

In this section, we will show how to define controllers in your angular applications.

在本节中,我们将展示如何在角度应用中定义控制器。

控制器和示波器的简单用法 (Simple Usage of Controller and Scope)

The following example demonstrates the Controller and Scope concept. Here, we will see how controllers sets the initial state of a $scope object.

下面的示例演示了控制器范围的概念。 在这里,我们将看到控制器如何设置$ scope对象的初始状态

angular-controller.html

angular-controller.html

<!DOCTYPE html>
<html><head>
<title> AngularJS - Controllers and Scope</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head><body><div ng-app="" ng-controller="blogController">First Name: <input type="text" ng-model="firstName"><br>Last Name: <input type="text" ng-model="lastName"><br><br>Full Name: {{firstName + " " + lastName}}<br>Blog Name : {{blogName }}</div><script>
//Controller
function blogController($scope) {$scope.firstName = "Pankaj",$scope.lastName = "Kumar",$scope.blogName = "JournalDev"
}</script></body>
</html>

You will see the following output in your browser.

您将在浏览器中看到以下输出。

演示地址

Application Details:

申请详情:

  • The ng-controller directive is used to attach the the Controller ( blogController, in the above example) to the DOM.ng-controller指令用于将Controller (在上面的示例中为blogController)附加到DOM。
  • A new child scope will be available as a parameter to the to the Controller’s constructor function as $scope.一个新的子作用域将作为$scope.的参数提供给Controller的构造函数$scope.
  • The Controller (blogController) set up the initial state of the $scope object. In our example, we set the initial state by attaching properties like firstName, lastName and blogName Controller( blogController )设置$scope对象的初始状态。 在我们的示例中,我们通过附加诸如firstNamelastNameblogName之类的属性来设置初始状态
  • We use ng-model directive to bind the input fields to the blogController properties.我们使用ng-model指令将输入字段绑定到blogController属性。

向控制器添加行为 (Adding behavior to Controller)

The following example demonstrates how to add behaviour to controllers. In Angular, we add behavior to the scope by attaching methods to the $scope object.

以下示例演示了如何向控制器添加行为 。 在Angular中,我们通过将方法附加到$scope对象来向范围添加行为。

In the following example we attach the addEmployee() method to add a new behaviour to the controller. A new item will be pushed to the array of names when we click on the Add button.

在下面的示例中,我们附加addEmployee()方法以向控制器添加新行为。 当我们单击“添加”按钮时,新项目将被推到名称数组中。

controller-methods.html

controller-methods.html

<!DOCTYPE html>
<html ng-app>
<head>
<title>AngularJS - Controllers and Scope</title>
<script type="text/javascript"src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>
<body><div ng-controller="EmployeeController">Employee Name : <input type="text" ng-model="newEmployee"/><button ng-click="addEmployee()">Add</button><h2>Employees</h2><ul><li ng-repeat="name in names"> {{ name }} </li></ul></div>
<script>function EmployeeController($scope) {$scope.names = [ "Elano Blumer","Iain Hume"];$scope.addEmployee = function() {$scope.names.push($scope.newEmployee);$scope.newEmployee = "";}}
</script>
</body>
</html>

Enter any name and click on add button. You will see the added names in the list below.

输入任何名称,然后单击添加按钮。 您将在下面的列表中看到添加的名称。

演示地址

Application Details:

申请详情:

  • The ng-controller directive is used to attach the the Controller ( EmployeeController, in the above example) to the DOM.ng-controller指令用于将Controller (在上面的示例中为EmployeeController)附加到DOM。
  • A new child scope will be available as a parameter to the to the Controller’s constructor function as $scope.一个新的子作用域将作为$scope.的参数提供给Controller的构造函数$scope.
  • The Controller (EmployeeController) set up the initial state of the $scope object. In our example, we set the initial state of the $scope with array of names.控制器( EmployeeController )设置$scope对象的初始状态。 在我们的示例中,我们使用names数组设置$ scope的初始状态。
  • We use ng-repeat directive to iterate over the employee names array.我们使用ng-repeat指令迭代雇员姓名数组。
  • We have attached addEmployee() method to add a behaviour to the above application. This line of code ($scope.names.push($scope.newEmployee);) will push new items in the input field to the names array when we click on the Add button.我们已经附加了addEmployee()方法来向上述应用程序添加行为。 这行代码( $scope.names.push($scope.newEmployee); )将在我们单击“ 添加”按钮时将输入字段中的新项目推送到名称数组。
  • The ng-click directive allows you to specify custom behavior when an element is clicked.ng-click指令允许您指定单击元素时的自定义行为。

在外部文件中使用控制器 (Using Controllers in External File)

The following example demonstrates how to store and include controllers in external files

以下示例演示了如何在外部文件中存储和包含控制器

In the example, we have created a JavaScript file ( placeController.js ) to store the controller used in the application.

在示例中,我们创建了一个JavaScript文件( placeController.js )来存储应用程序中使用的控制器。

placeView.html

placeView.html

<!DOCTYPE html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js">
</script>
</head>
<body><div data-ng-controller="PlaceController">Country:<input type="text" ng-model="countryName"/>City:<input type="text" ng-model="cityName"/><button ng-click="addItem()">Add</button><ul><li ng-repeat="item in list">{{ item.country +"  : "+ item.city}}</li></ul></div><script src="placeController.js"></script>
</body>
</html>

The controller used in the application, placeController.js

应用程序中使用的控制器placeController.js

placeController.js

placeController.js

function PlaceController($scope) {$scope.list = [{ country: 'India',   city: 'New Delhi' },{ country: 'France',  city: 'Paris' },{ country: 'USA',     city: 'Washington DC' },{ country: 'England', city: 'London' }];$scope.addItem = function () {$scope.list.push({ country: $scope.countryName,city: $scope.cityName });};
}

You will see the following output in your browser. Enter country name and city and then click on add button. You will see the added names in the list shown below.

您将在浏览器中看到以下输出。 输入国家名称和城市,然后单击添加按钮。 您将在下面显示的列表中看到添加的名称。

演示地址

Application Details:

申请详情:

  • The ng-controller directive is used to attach the the Controller ( PlaceController stored in placeontroller.js, in the above example) to the DOM.ng-controller指令用于将Controller (在上面的示例中, PlaceController存储在placeontroller.js中)附加到DOM。
  • A new child scope will be available as a parameter to the to the Controller’s constructor function as $scope.一个新的子作用域将作为$scope.的参数提供给Controller的构造函数$scope.
  • The Controller (PlaceController) set up the initial state of the $scope object.Controller( PlaceController )设置$scope对象的初始状态。
  • We use ng-repeat directive to iterate over the object list.我们使用ng-repeat指令遍历对象列表。
  • We have attached addItem() method to add a behaviour to the above application. This line of code ($scope.list.push({ country: $scope.countryName, city: $scope.cityName });) will push new items in the input field to the list of objects when we click on the Add button.我们已经附加了addItem()方法来向上述应用程序添加行为。 这行代码( $scope.list.push({ country: $scope.countryName, city: $scope.cityName }); )将在我们单击“ 添加”按钮时将输入字段中的新项目推送到对象列表中。
  • The ng-click directive allows you to specify custom behavior when an element is clicked.ng-click指令允许您指定单击元素时的自定义行为。

In this tutorial, we demonstrated the use of controllers to set up the initial state of the $scope object and add behavior to that object.

在本教程中,我们演示了如何使用控制器来设置$ scope对象的初始状态并向该对象添加行为。

That’s all for now and you will see more angular features in the coming posts.

到此为止,您将在以后的文章中看到更多的角度特征。

翻译自: https://www.journaldev.com/6033/angularjs-controller-scope-and-view-tutorial-example

实现视图示例

实现视图示例_AngularJS控制器,范围和视图教程示例相关推荐

  1. ASP.NET MVC 5 - 将数据从控制器传递给视图

    ASP.NET MVC 5 - 将数据从控制器传递给视图 原文:ASP.NET MVC 5 - 将数据从控制器传递给视图 在我们讨论数据库和数据模型之前,让我们先讨论一下如何将数据从控制器传递给视图. ...

  2. 【转】Scott_ASP.NET MVC框架(第三部分) 把ViewData从控制器传到视图

    Scott的MVC框架系列,非常好的MVC学习资料.很适合初学者阅读.做为一个系列转载过来,纯粹是为了学习方便. 文章转自:Scott Guthrie 博客中文版 [原文地址]ASP.NET MVC ...

  3. IOS之导航控制器与表视图

    7.1 导航控制器 7.2 创建第一级控制器 7.3 第一个二级控制器 7.4 第一个三级控制器 7.5 第二个二级表控制器 7.6 第三个二级表控制器 7.7 第四个二级表控制器 7.8 第五个二级 ...

  4. 视图中获取控制器中数据的方式

    在视图中获取控制器中数据的方式有两种: 一种是在控制器的操作中渲染视图时候分配模板数据,这种方式叫推送. 另外一种是在视图中手动获取控制器中的数据,包括控制器中的属性和方法及控制器id,通过$this ...

  5. IOS开发-表视图LV3导航控制器

    学到这里感觉有点难了,其实这篇文章再草稿箱里放了好久了~ 最近对于学习的热情下降了.这不行-抓紧学习走起! 在这一章节的学习中主要针对导航控制器及表视图来建立多视图的应用, 首先要了解一些概念-- 1 ...

  6. ASP.Net MVC开发基础学习笔记:三、Razor视图引擎、控制器与路由机制学习

    一.天降神器"剃须刀" - Razor视图引擎 1.1 千呼万唤始出来的MVC3.0 在MVC3.0版本的时候,微软终于引入了第二种模板引擎:Razor.在这之前,我们一直在使用W ...

  7. Razor视图引擎、控制器与路由机制学习

    1.1 千呼万唤始出来的MVC3.0 在MVC3.0版本的时候,微软终于引入了第二种模板引擎:Razor.在这之前,我们一直在使用WebForm时代沿留下来的ASPX引擎或者第三方的NVelocity ...

  8. ASP.Net MVC开发基础学习笔记(3):Razor视图引擎、控制器与路由机制学习

    一.天降神器"剃须刀" - Razor视图引擎 1.1 千呼万唤始出来的MVC3.0 在MVC3.0版本的时候,微软终于引入了第二种模板引擎:Razor.在这之前,我们一直在使用W ...

  9. [转]ASP.Net MVC开发基础学习笔记(3):Razor视图引擎、控制器与路由机制学习

    [出处]http://www.cnblogs.com/edisonchou/p/3923475.html 关于机制的介绍,讲得不错,觉得可以参考着学习一下 1.1 千呼万唤始出来的MVC3.0 在MV ...

最新文章

  1. LINUX学习笔记高度浓缩版之一 :用户管理、启动过程、硬盘管理
  2. 最近实际项目中遇到的技术问题与解决思路
  3. gihosoft android 教程,Gihosoft Free Android Data Recovery
  4. html走马观花效果,走马观花台湾行 用EF-S 10-18来记录风景
  5. google 插件_google这4款插件我每天都用,省时无数
  6. Geatpy框架使用基于NSGA-II算法的多染色体多目标进化算法案例(moea_psy_NSGA2_templet)
  7. 泸州职称计算机,2018年11月四川泸州职称计算机考试10月8日开始报名
  8. linux多线程学习(七)——实现“生产者和消费者”
  9. ajax响应码,ajax处理响应(三)(示例代码)
  10. 二十一天学通C语言:使用const声明指针变量
  11. TextMesh Pro不能显示中文的解决办法是创建字贴图,常用汉字3500
  12. 百度之星程序设计大赛
  13. win7家庭版计算机桌面,Win7 home basic家庭普通版显示桌面图标的方法
  14. 可变条码打印软件如何制作黑底白字条形码
  15. 微软Windows 8 非常实用的12个技巧
  16. VOIP流中使用CNN-LSTM下对QIM的隐写分析方法
  17. numpy 是否为零_玩数据必备 Python 库:Numpy 使用详解
  18. CocoWu‘s Summer English Study Summery
  19. Hostapd.conf详细释义
  20. 计算机怎么学要记笔记,留法十全大补汤 | 学姐告诉你在法国上课如何记笔记,复习,考试!...

热门文章

  1. 电影'社交网络'获金球奖最佳影片,最佳编剧,最佳导演,最佳配乐奖
  2. 在老ASP中使用对象的对象生存期问题
  3. [转载] [python标准库]math——数学函数
  4. 迷人的bug--torch.load
  5. javaScript 对象访问属性的另一种方式
  6. 【pwnable.kr】passcode
  7. ISP图像调试工程师——3D和2D降噪(熟悉图像预处理和后处理技术)
  8. 【bzoj 入门OJ】[NOIP 热身赛]Problem C: 星球联盟(并查集)
  9. Ajax基础知识《一》
  10. VS 安装部署项目自解压程序解压后按顺序执行多个程序