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:



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 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.



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:



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



THREE QUERIES offers easy access to information about SharePoint and associated technologies, project management, agile and scrum methodologies that helps developers, administrators, architects, technical managers, business analysts and end users. It has grown from there. We provide an important knowledge base for those involved in managing, architecture and developing software projects of all kinds. With weekly/daily exclusive updates, we keep you in touch with the latest business, management, technology thinking.

WE ARE CONNECTED ~ Follow us on social media to get regular updates and opinion on what's happening in the world of SharePoint, front-end, back end web technologies and project management. If you like this article, please share it and follow us at Facebook, Twitter, Instagram and LinkedIn


About Satyendra Mishra

Microsoft certified, motivated, energetic and accomplished SharePoint Consultant and Architect with 13+ years of work experience in Management, Architecture, Analytics, Development and Maintenance. He has been fortunate to be a part of over 20+ Web/Mobile/Software SharePoint and .Net projects delivery with various companies across different industry sectors. This has provided him a valuable insight and experience especially in successful implementation of technology solutions. Having very strong System Architecture, Technological, Management & Analytical Skills, Knowledge of Office 365, SharePoint, .Net, JavaScript, React, Angular, Azure, SQL. In 2016, he decided to start sharing the knowledge back to society in the form for content writing and blogging.
View all posts by Satyendra Mishra →

Leave a Reply

Your email address will not be published. Required fields are marked *