Skip to main content

ControllerAs Syntax in AngularJS - Best Practice

Here we will learn why we need ControllerAs in angularjs

All we know communication between controller and View happens via $scope.

Let have a code below

AngularJS:

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.firstname = "Devesh";

});
</script>


HTML :

<div ng-app="myApp" ng-controller="myCtrl">
    Name: <input ng-model="firstname">
    <h1>{{firstname}}</h1>
</div>



Here we have few issues

1. Binding everything to $scope, will increase  watch list of the $digest loop. which will cause performance issue so angularJs include ControllerAs syntax.


ControllerAs syntax

AngularJS:

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    this.firstname = "Devesh";

});
</script>

HTML 

<div   ng-app="myApp"  ng-controller="MyCtrl as mctrl">
  <h1 ng-bind="mctrl.firstname "></h1>
</div>


here we are not using $scope and we are using  ng-controller="MyCtrl as mctrl"

and mctrl.firstname to access the data.

2. Second Problem :  look at code below

AngularJs

<script>
var app = angular.module('myApp', []);
app.controller('ParentCntrl', function($scope) {
    $scope.firstname = "Devesh";

});

app.controller('ChildCntrl', function($scope) {
    $scope.firstname = "Devesh2";

});

app.controller('NestedCntrl', function($scope) {
    $scope.firstname = "Devesh3";

});
</script>


Explanation :

We have defined 3 controllers ParentCntrl,ChildCntrl,NestedCntrl
and having  $scope.firstname in each controller

HTML :

<div ng-controller="ParentCntrl">
  {{ firstname }}
  <div ng-controller="ChildCntrl">
    {{ firstname }}
    <div ng-controller="NestedCntrl">
      {{ firstname }}
    </div>
  </div>
</div>

Here in above HTML we are printing firstname at each div
but here code appeard bad as it appears we are printing firstname but for which controller, code is confusing so it is bad
Practice


Solution :

instead having $scope try to use "this" in controller and use controllerAs syntax

HTML:

<div ng-controller="ParentCntrl as P1">
  {{ P1.firstname }}
  <div ng-controller="ChildCntrl as C1">
    {{ C1.firstname }}
    <div ng-controller="NestedCntrl as n1">
      {{ n1.firstname }}
    </div>
  </div>
</div>

Here in above HTML we can easily identify the relation between each controller. code is clear and easy to understand

Comments

Contact Form

Name

Email *

Message *

Popular posts from this blog

Exporting DataTable to Excel in C# Using Interop

Introduction I would like to share a utility that can be used to export a datatable to Excel file using C#. We have various approaches to perform this activity. In ASP.NET we can do this by changing the Content type to xls but in C# .Net we might have various other approaches. Through this article we will learn how to export a data table to Excel using Interop. Code and Steps Here in this application we will use a Sample datatable and then learn how to export data to an Excel file. We will learn the following things in this article: Creating Excel file using C# Writing data to cells Formatting data to cells Working with Excel range 1. Adding References First we need to add a reference for Microsoft.office.interop.Excel as in the following: 2. Adding sample data table to the code Use the following code to add the sample data table:     static   DataTable  GetTable()     {       ...

Binding CheckBoxList in ASP.Net MVC

Blogspot Introduction I would like to share how to bind the checkbox list in MVC. I will use a Model (a class file) to define various attributes for checkboxes. For a basic understanding of MVC kindly use the following link: ASP.NET MVC Overview The following is the procedure. 1. Creating Model We created a "SubjectModel" class under the Models folder and defined the following two properties: Subject: to display text Selected: to display check/uncheck 2. Creating the controller We created a Controller "BindingCheckBoxController" under the controllers folder We created an Action having the name = "DisplaycheckBoxes" We created a list of Subjects (Subject model class) Returning a list of subjects to the View. public  ActionResult DisplayCheckboxes() {     List<SubjectModel> listsubject =  new  List<SubjectModel>();     listsubject.Add( new  SubjectModel( "Physics" , true )); ...

Indexes In SQL SERVER in Hindi Part 1