AngularJS Controllers and Scopes

In other words, 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 data in AngularJS applications. These  are nothing but regular JavaScript Objects created by standard JavaScript object constructor.

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

Let’s see it’s example:

Output:

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

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

$scope is owner of 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 controller properties “firstName” and “lastName”.

Why do we need Controllers

A question is arising here why do we need controller? Let’s understand single line definitions of followings:

AngularJS Model : –  Model represents data in AngularJS application

AngularJS View:- View represents HTML part of the application

AngularJS Controller :- Controller handles the flow and operations between data and HTML. It is  normal JavaScript logic.

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

One important application object that is available at all three layers is Scope denoted by $Scope. We’ll learn more about AngularJS Scope later in this article.

Controller Methods

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

Let’s see an example and understand it’s implementation and execution.

Output:

Explanation:

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

As we are using an application object $Scope a multiple locations, lets understand it as well.

AngularJS Scopes

The scope is a JavaScript object with 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 you are dealing with. But if application is large and there are multiple scopes where certain sections have access to fixed set of available scopes. then?

Basically, there are two type of scopes in AngularJS application:

  1. Root Scope

All applications have $rootScope . This scope is created on the HTML element contains ng-app directive and available for entire application.

  1. Current Scope

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

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

Let’s understand it using an example:

Output:

Explanation:

Here the value of color property in controller part is Green otherwise it’s Red throughout the application due to availability of $rootScope.

 

Be  Connected…

Satyendra Mishra

,

About Satyendra Mishra

Satyendra is a Technical Project Manager with 12+ years of experience on Project Management, Microsoft Technologies including ASP.NET, ASP.NET MVC, C#, SharePoint (On premises & Office 365), JavaScript Framework (AngularJS) and related technologies. He is a person with belief, sky is limit for a hard working and dedicated individual. That’s why he devoted himself to field of Software Development and now both are growing with each other. Every year for him turned out to be best year and he received enormous support and love from his companies and his clients from around the world. Now with vast knowledge of software development and its outsourcing process, he decided to move himself into knowledge sharing. Keep a fact in your mind about the comfort level of your clients and this will make you king/queen of your area.
View all posts by Satyendra Mishra →