未加星标

AngularJS - Controllers And Scopes

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

AngularJS Controllers

There is a logic behind AngularJS View and AngularJS Models .

In this article, we'll see the behavior and usage of controllers in AngularJS. It controls/manipulates the data in AngularJS Applications. These are nothing but the regular javascript Objects created by standard JavaScript object constructor.

The main aspect, which controls AngularJS are controllers itself. To define AngularJS controllers, we use a directive ng-controller.

Let's see its example.


AngularJS - Controllers And Scopes

Output


AngularJS - Controllers And Scopes

Explanation

Here, ng-app defines AngularJS Application and name is given as "smApp". Ultimately, it has created a root scope of Application as well.. This AngularJS Application is within the scope of this <div> only.

ng-controller defines AngularJS controller mapped with "smController" . "smController" is a JavaScript function. AngularJS will invoke the controller with $scope object.

$scope is an owner of the Application variables and functions. Controller creates two properties "firstName" and "lastName" with the help of $scope Application object.

ng-model AngularJS directive is binding HTML input fields with the controller properties "firstName" and "lastName".

Why do we need Controllers?

A question is arising here i.e. why do we need controller? Let's understand single line definitions of the following. AngularJS Model- Model represents the data in AngularJS Application AngularJS View- This view represents HTML part of the Application. AngularJS Controller- This c ontroller handles the flow and operations between data and HTML. It is a normal JavaScript logic.

If we want to manipulate the data in AngularJS Model and want to assign or fetch it from HTML portion of an Application i.e. AngularJS View, a logic is required. This logic is written in JavaScript and is called as AngularJS Controller.

One important Application object, which is available at all the three layers is Scope denoted by $Scope. We'll learn more about AngularJS Scope later.

Controller Methods

In the last example of this article, we have seen that the Application properties (e.g. firstName and lastName) can be set inside controller but, how to define the methods in AngularJS Application.

Let's see an example, understand it's implementation and execution.


AngularJS - Controllers And Scopes

Output


AngularJS - Controllers And Scopes

Explanation

In this example, we can see a method fullName() is called in AngularJS Application, which is defined in controller part of an Application.

As we are using an Application object $Scope at the multiple locations, let's understand it as well.

AngularJS Scopes

The scope is a JavaScript object with the properties and methods, which are available for both model and controller.

If an Application is small, there is no issue to find out the scope which you are dealing with. If the Application is large, there are multiple scopes, where certain sections have access to the fixed set of available scopes.

Basically, there are two types of scopes in AngularJS Application, which are-

Root Scope

All Applications have $rootScope. This scope is created on HTML element, which contains ng-app directive and is available for an entire Application.

Current Scope

This $scope is created on HTML element, which contains ng-controller directive.

If root scope and current scope both have a property of the same name, the value of current scope will be used by the Application.

Let's understand it, using an example, given below.


AngularJS - Controllers And Scopes

Output


AngularJS - Controllers And Scopes

Explanation

Here, the value of color property in controller part is Green, else it's Red throughout the Application due to the availability of $rootScope.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: AngularJSJavaScriptHTMLJava
分页:12
转载请注明
本文标题:AngularJS - Controllers And Scopes
本站链接:http://www.codesec.net/view/482486.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(28)