AngularJS - Controllers And Scopes

In this article, you will learn about controllers and scopes, using AngularJS.

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.



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



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-

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

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



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