Overview
In this article, we will see AngularJS Controller as syntax. Thus, we used $scope mechanism to make members of the controllers, available in their respective views. We will see the details in this article. Thus, let's start-
For more articles on AngularJS, kindly refer-
- Overview Of AngularJS
- Modules And Controller In AngularJS
- Controllers in AngularJS
- AngularJS ng src Directive
- AngularJS ng-Repeat Directive
- Handling Events In AngularJS
- How To Use Two-Way Data Binding In AngularJS
- Filters In AngularJS
- Sorting Data In AngularJS
- Sorting Rows By Table Header In AngularJS
- Creating Custom Filters In AngularJS
- ng-Hide And ng-Show In AngularJS
- ng-init Directive In AngularJS
- Search And MultiSearch In AngularJS
- ngInclude Directive In AngularJS
- $http Service In AngularJS
- Consuming ASP.NET WebService In AngularJS
- AnchorScroll Service In AngularJS
Lets create an empty Application. Add a simple HTML file. Now, add a JavaScript file, name it as script.js.
In this script file, drag drop the reference of angularmin.js file reference, as given below-
- /// <reference path="angular.min.js" />
- var app = angular.module("Demo", []);
- app.controller("mainController", function ($scope) {
- $scope.message = "Hello C-sharpcorner";
- });
- <script src="scripts/angular.min.js"></script>
- <script src="scripts/script.js"></script>
- <html ng-app="Demo">
- <h1 ng-controller="mainController">
- <h1 ng-controller="mainController">
- {{message}}
- </h1>

We got Hello Csharpcorner output now. When you see the controller, we are injecting $scope object to members of the controllers, available to their respective views, which we don’t need, so we can get rid of $scope object here and use-
- var app = angular.module("Demo", []);
- app.controller("mainController", function () {
- this.message = "Hello C-sharpcorner";
- });
Now, you will pass that alias, which you had used in your binding expression property, as given below-
- <h1 ng-controller="mainController as mainCtrl">
- {{mainCtrl.message}}
- </h1>

You can see, we got the same output without using $scope object here . Let's see, how to use Controller as syntax. To see that, we will use the same example DemoApplication, which we used in our previous Applications also.
In this example, the script file contains the code, given below-
- /// <reference path="angular.min.js" />
- /// <reference path="angular-route.min.js" />
- var app = angular.module("Demo", ["ngRoute"])
- .config(function ($routeProvider, $locationProvider) {
- $routeProvider
- .when("/home", {
- templateUrl: "Templates/home.html",
- controller: "homeController"
- })
- .when("/courses", {
- templateUrl: "Templates/courses.html",
- controller: "coursesController"
- })
- .when("/students", {
- templateUrl: "Templates/students.html",
- controller: "studentsController"
- })
- .when("/students/:id", {
- templateUrl: "Templates/StudentDetail.html",
- controller: "StudentDetailController"
- })
- .otherwise({
- redirectTo: "/home"
- })
- $locationProvider.html5Mode(true);
- })
- .controller("homeController", function ($scope) {
- $scope.message = "Home Page";
- })
- .controller("coursesController", function ($scope) {
- $scope.courses = ["c#", "SQL", "Oracle"];
- })
- .controller("studentsController", function ($scope, $http) {
- $http.get("StudentService.asmx/GetAllStudents")
- .then(function (response) {
- $scope.students = response.data;
- })
- })
- .controller("StudentDetailController", function ($scope, $http, $routeParams) {
- $http({
- url: "StudentService.asmx/GetStudents",
- params: { id: $routeParams.id },
- method:"get"
- })
- .then(function (response) {
- $scope.student = response.data;
- })
- });
- controller: "homeController as homeCtrl"
- controller: "coursesController as coursesCtrl"
- controller: "studentsController as studentsCtrl"
- controller: "StudentDetailController as StudentDetailCtrl"
- .controller("homeController", function () {
- this.message = "Home Page";
- })
- .controller("coursesController", function () {
- this.courses = ["c#", "SQL", "Oracle"];
- })
- .controller("studentsController", function ( $http) {
- $http.get("StudentService.asmx/GetAllStudents")
- .then(function (response) {
- $scope.students = response.data;
- })
- })
- .controller("StudentDetailController", function ( $http, $routeParams) {
- $http({
- url: "StudentService.asmx/GetStudents",
- params: { id: $routeParams.id },
- method:"get"
- })
- .then(function (response) {
- this.student = response.data;
- })
- <li ng-repeat="course in coursesCtrl.courses">
- {{course}}
- </li>
- <h1>{{homeCtrl.message}}</h1>
- <table style="border:1px solid black">
- <tr>
- <td>Id</td>
- <td>{{StudentDetailCtrl.student.id}}</td>
- </tr>
- <tr>
- <td>Name</td>
- <td>{{StudentDetailCtrl.student.name}}</td>
- </tr>
- <tr>
- <td>City</td>
- <td>{{StudentDetailCtrl.student.city}}</td>
- </tr>
- <tr>
- <td>Gender</td>
- <td>{{StudentDetailCtrl.student.gender}}</td>
- </tr>
- </table>
- <a href="students">Back To List </a>
- <li ng-repeat="student in studentsCtrl.students">
- <a href="students/{{student.id}}">
- {{student.name}}
- </a>
- </li>

Click Courses. You will see the output but when we click students, we found no data has populated.

When you click on developer tools options, you will see the error also.

This is because we had used this keyword in reposnse data in StudentsController and StudentsDetailsController.

Now, here this.students = response.data is actually pointing to the Window object and not to the instance of the their respective controllers. Due to this, we are not able to load the data from Webservice. To make it work, we will create a variable.
- var vm = this;
- vm.students = response.data;
- var vm = this;
- vm.student = response.data;
- /// <reference path="angular.min.js" />
- /// <reference path="angular-route.min.js" />
- var app = angular.module("Demo", ["ngRoute"])
- .config(function ($routeProvider, $locationProvider) {
- $routeProvider
- .when("/home", {
- templateUrl: "Templates/home.html",
- controller: "homeController as homeCtrl"
- })
- .when("/courses", {
- templateUrl: "Templates/courses.html",
- controller: "coursesController as coursesCtrl"
- })
- .when("/students", {
- templateUrl: "Templates/students.html",
- controller: "studentsController as studentsCtrl"
- })
- .when("/students/:id", {
- templateUrl: "Templates/StudentDetail.html",
- controller: "StudentDetailController as StudentDetailCtrl"
- })
- .otherwise({
- redirectTo: "/home"
- })
- $locationProvider.html5Mode(true);
- })
- .controller("homeController", function () {
- this.message = "Home Page";
- })
- .controller("coursesController", function () {
- this.courses = ["c#", "SQL", "Oracle"];
- })
- .controller("studentsController", function ($http) {
- var vm = this;
- $http.get("StudentService.asmx/GetAllStudents")
- .then(function (response) {
- vm.students = response.data;
- })
- })
- .controller("StudentDetailController", function ( $http, $routeParams) {
- var vm = this;
- $http({
- url: "StudentService.asmx/GetStudents",
- params: { id: $routeParams.id },
- method:"get"
- })
- .then(function (response) {
- vm.student = response.data;
- })
- });

As you can see from the output, we got the list of the students from our database, which means our Webservice is working fine. When you click the detail of it, the particular ID also displays the property.

Thus, we got the output.
Conclusion
This was about AngularJS Controller as syntax. Hope this article was helpful!!

Join the conversation! Your thoughts help the community grow.