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-
Introduction
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-
Now, add a module name and controller name. In this controller, attach a $scope object and in this, display a Hello Message, as given below-
- var app = angular.module("Demo", []);
- app.controller("mainController", function ($scope) {
- $scope.message = "Hello C-sharpcorner";
-
- });
Now, go back to our HTML Page, which we had created earlier, add the reference of Angular script file and our script file.
- <script src="scripts/angular.min.js"></script>
-
- <script src="scripts/script.js"></script>
Now, add ng-controller name and ng-app name in HTML tag and just bind the message property.
Our module name is Demo and Controller name is mainController, which is given below-
- <h1 ng-controller="mainController">
Now, let's bind the message property here-
- <h1 ng-controller="mainController">
-
- {{message}}
- </h1>
Now, let's save the changes and lets reload the page. We will see the output, given below-
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";
-
- });
In our HTML page, we are using ng-controller directive and we are specifying the controller name too. We will use a keyword and alias , which you want to use.
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>
Save the changes and reload the page, given below-
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-
-
-
-
-
-
- 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;
-
- })
-
- });
Now, we are going to rename the Controller name, as given below-
- controller: "homeController as homeCtrl"
-
- controller: "coursesController as coursesCtrl"
-
- controller: "studentsController as studentsCtrl"
-
- controller: "StudentDetailController as StudentDetailCtrl"
Thus, this was our first change. Now, the next change is to get rid of $scope object and use this, as given below-
- .controller("homeController", function () {
- this.message = "Home Page";
- })
-
- .controller("coursesController", function () {
- this.courses = ["c#", "SQL", "Oracle"];
- })
Now, notice, we have StudentsController and StudentsDetailsController, as we can get rid of $scope object from the parameter but we can get rid of where we had used it in our response, as we are using http GET request. We will remove and replace with this. See how it behaves. Thus, for our StudentsController and StudentsDetailsController code is given below-
- .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;
-
- })
We need to make the changes in our respective views also. Let's go back to our courses.html view and now we had used coursesCtrl as an alias. We will put in the ng-repeat directive, which we had lopped through those records, as given below-
- <li ng-repeat="course in coursesCtrl.courses">
- {{course}}
-
- </li>
Make changes in Home View also, as given below-
- <h1>{{homeCtrl.message}}</h1>
Similarly, lets do it for the students details, as given below-
- <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>
Similarly, do it for students, as given below-
- <li ng-repeat="student in studentsCtrl.students">
- <a href="students/{{student.id}}">
- {{student.name}}
- </a>
- </li>
Save the changes and run the Application.
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.
Now, attach this variable to students, as given below-
- vm.students = response.data;
Similarly, for students details controller, use the code given below-
- var vm = this;
- vm.student = response.data;
Thus, our final script.js code is given below-
-
-
-
-
-
- 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;
- })
- });
Now, let's save the changes and reload the page, as given below-
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!!