21 Point Cheat Sheet on AngularJS Controller and the $scope Object

This article is a 21-point cheat sheet on the AngularJS Controller and the $scope object. These points can be used as quick notes when working with two important components of the Angular app Controller and the $scope object.

Notes on $scope object

$scope is a glue between the View and the Controller. It connects a Controller with the View.

scope

  1. $scope serves as the glue between the Controller and the View.

  2. The $scope is the connection between the HTML and the View.

  3. The View and the model both have access to the $scope.

  4. In the context of MVC, $scope can be seen as the ViewModel.

  5. $scope provides the execution context for the DOM and the expression.

  6. $scope provides an execution context in which the DOM element is bound.

  7. $scope is the source of the truth.

  8. $scope is modified when the View changes and the View is modified when $the scope changes its value.

  9. The $scope object is a plain JavaScript object. We can add and remove a property as required.

  10. $scope holds data and functions from the Controller that should be displayed and executed in the View.

  11. The $rootScope is the eventual parent of all the $scope.

  12. $rootScope is the top-most scope in a DOM element with the ng-app directive.

  13. In angular all the $scope are created with prototypal inheritance.

  14. $scope has access to their parent scope.

  15. $scope contains data and the functionality to be used to render the View.

  16. For each Controller created a new $scope is created.

  17. It is ideal to contain the application logic in the Controller and the data in the $scope of the Controller.

  18. When $the scope object is not needed in the View, the scope will be cleaned up and destroyed.

  19. Directives do not have their own scope but with some exceptions ng-controller and ng-repeat do.

  20. When angular starts running all the $scope are attached to the View.

  21. $scope passes data and behavior to the View.

Example: Adding property to $rootScope

To add a property to $rootScope directly, pass a $rootScope as the parameter to the Controller as shown below. However it is not advisable to add properties to $rootScope directly.

  1. var myApp = angular.module('myApp', []);  
  2.   
  3. myApp.controller('RootController', ['$rootScope'function ($rootScope) {  
  4.   
  5.    $rootScope.name = "dj";  
  6.   
  7. }]); 

On the DOM added property of $rootScope can be rendered in the expression as shown below:

  1. <div ng-controller="RootController">  
  2.    <h1>{{name}}</h1>  
  3. </div> 

Another way you can add a property to the $rootScope object directly using the run method on the application module.

  1. angular.module('myApp', [])  
  2. .run(function ($rootScope) {  
  3.    $rootScope.name = "World";  
  4. }); 

To use this you don't need a Controller to attach with the View and can be used in the application as shown below:

  1. <div ng-app="myApp">  
  2.    Hello {{name}}  
  3. </div> 

Notes on the Controller

The Controller adds behaviour and data to the $scope object. It is created on the View using this ng-controller directive. Each Controller has its own $scope object.

scope object

  1. It's a JavaScript constructor function that is used to augment the $scope object.

  2. The Controller takes $scope as parameter and attaches data and behavior to this.

  3. The Controller is attached to the DOM using the ng-controller directive.

  4. Each Controller has its own child $scope.

  5. The Controller sets up the initial state of the $scope object.

  6. The Controller adds behavior to the $scope object.

  7. Do not use a Controller to manipulate the DOM, it should only contain the business login, not the DOM manipulation.

  8. Do not use a Controller to format the input.

  9. Do not use a Controller to filter the output.

  10. Do not use a Controller to share the code across Controllers.

  11. Never create the service instance inside the Controller.

  12. A Controller should mainly contain simple business logic.

  13. It should not try to do many things.

  14. All the common functionality should go in the service and the later service can be injected into the Controller via dependencies.

  15. The Controller name starts with a capital letter and ends with Controller.

  16. Whenever a new Controller is created on the page, angular passes a $scope object to it.

  17. Custom actions to be called from the View can be created as the behavior of the function in the Controller augmenting the $scope object.

  18. A Controller can be attached to a different level of the DOM hierarchy, hence it creates a hierarchy of the $scope.

  19. $scope in a child Controller can access and override data and behavior attached to $scope of the parent Controller.

  20. Other directives or services to be used in the Controller must be passed as a parameter in the Controller function constructor.

  21. A Controller should provide data and business logic to a specific View.

Example: Creating a simple Controller

Let us create a simple Controller with two functions as behavior and one data. Functions are either adding or subtracting 1 from the counter attached as a property to the $scope object.

  1. var myApp = angular.module('myApp', []);  
  2. myApp.controller('mycontroller', ['$scope'function ($scope) {  
  3.   
  4.     $scope.counter =0;  
  5.     $scope.add = function () { $scope.counter += 1;}  
  6.     $scope.sub = function () { $scope.counter -= 1; }  
  7. }]); 

On the DOM a Controller can be used as shown below:

  1. <div ng-controller="mycontroller">  
  2.    <h1>{{counter}}</h1>  
  3.    <button class="btn btn-info" ng-click="add()">+</button>  
  4.    <button class="btn btn-danger" ng-click="sub()">-</button>  
  5. </div> 

When you use a Controller for another DOM element, a new instance of the Controller will be created.

  1. <div ng-controller="mycontroller">  
  2.    <h1>{{counter}}</h1>  
  3.    <button class="btn btn-info" ng-click="add()">+</button>  
  4.    <button class="btn btn-danger" ng-click="sub()">-</button>  
  5. </div> 

On running the application you will find that both of the div will have different values for the $scope data.

Example: $scope inheritance

Let us create a module with two Controllers, ParentControler and ChildController. As you see, ChildController is overriding the age property on the $scope object.

  1. var myApp = angular.module('myApp', []);  
  2.   
  3. myApp.controller('ParentController', ['$scope'function ($scope) {  
  4.   
  5.     $scope.name = "dj";  
  6.     $scope.age = 32;  
  7.       
  8. }]);  
  9. myApp.controller('ChildController', ['$scope'function ($scope) {  
  10.   
  11.       
  12.     $scope.age = 22;  
  13.     $scope.grade = "A+";  
  14.   
  15. }]); 

In the View we have created a ChildController inside the ParentController div. In the ChildController $scope, there is no property called name. So angular will traverse to the ParentController to find the value of the name property. Also you can see that the age property is overridden in the ChildController, so the div attached with the ChildController will render the overridden value of the age property.

  1. <div class="container">  
  2.   
  3.   <div ng-controller="ParentController">  
  4.        <h2>{{age}}</h2>  
  5.        <div ng-controller="ChildController">  
  6.            <h1>{{name}}</h1>  
  7.            <h2>{{age}}</h2>  
  8.            <h3>{{grade}}</h3>  
  9.       </div>  
  10.  </div> 

I hope, these 21 points about the $scope object and the Controller should help you in working with AngularJS. Thanks for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all