In the 10th day of AngularJS article series, we are going to learn next key players/concept of AngularJS, understanding the concept of Custom Filters in AngularJS. Before moving to key players/concepts of AngularJS, please read my previous articles:
- Introduction to AngularJS – Day 1
- Introduction to AngularJS – Day 2
- Introduction to AngularJS – Day 3
- Introduction to AngularJS – Day 4
- Introduction to AngularJS – Day 5
- Introduction to AngularJS – Day 6
- Introduction to AngularJS – Day 7
- Introduction to AngularJS – Day 8
- Introduction to AngularJS – Day 9
- Introduction to AngularJS – Day 10
Controller
In AngularJS framework controllers are the fundamental entry points to the presentation layer. We need some way to get data to and from the HTML document, that time we need to create a controller. Controller acts as ‘brain’ for a view.
- It defines the properties and methods.
- It defines the business logic.
- It handles the show/hide controls and data in view.
- It handle events triggered by view.
- It knows how to retrieve data to view.
- It interacts with the view using the $scope object.
For using this created controller in view HTML element we used ‘ng-controller’ directive, it tells the view which controller we are using. Using the $scope you can access the properties and methods of controller. $scope is injected in controller. The controllers contain all the functions that we need to call a page.
In order to create a controller first we need to create a module. In AngularJS framework, a module is like a namespace in C# or java. We see the concept of module in previous article. Using this module we can create directives, filters, controllers, constant, etc.
![code]()
The above image shows, using module you can create controllers, config, constructors, etc. The following is the syntax of creating a controller with the help of module.
- <scripttype="text/javascript">
- var app = angular.module("myApp", []); app.controller("ctrlName", function ($scope) { });
- </script>
How we use this created controller in HTML elements, using the ‘ng-controller’ directive,
HTML code
- <!DOCTYPEhtml>
- <htmlng-app="myApp">
-
- <head>
- <title>Controllers in AngularJS</title>
- <scriptsrc="Scripts/angular.min.js">
- </script>
- </head>
-
- <body>
- <divng-controller="ctrlName">
- <h1>Welcome to C# Corner!</h1>
- <h3>Creating Controller in AngularJS</h3>
-
- </div>
- <scripttype="text/javascript">
- var app = angular.module("myApp", []); app.controller("ctrlName", function ($scope) { });
- </script>
- </body>
-
- </html>
You can create a ‘.js’ file and put this JavaScript or module and controller code and include in HTML file or you can create in same HTML file also as we saw in above image. Do not use an AngularJS controller for the following:
- Presentation logic, it will negatively affect the testability if we add the presentation logic in controller. A controller only contain the application logic.
- To format the input, instead of formatting in the controller, use AngularJS form controls instead use an AngularJS filters as we discussed in previous article.
Now we are going to create a simple example of controller. We are creating a module and controller and defining a property and methods / functions. Firstly, we are going to create two buttons and a <span>. We are creating a property named ‘laptop’ and this property is binding to the <span> with the help of {{expression}} by default it set to ‘Your Laptop Model is Samsung’ and using buttons we are changing the values of this property ‘laptop’. For better understanding we are going to create a separate file for JavaScript code. The following is the JavaScript code for creating module, controller, property and methods:
controller.js
-
- var app = angular.module("myApp", []);
- app.controller("LaptopController", function($scope)
- {
-
- $scope.laptop = 'Your laptop model is Samsung!';
-
- $scope.dell = function()
- {
- $scope.laptop = 'Your laptop model is Dell!';
- };
-
- $scope.sony = function()
- {
- $scope.laptop = 'Your laptop model is Sony!';
- };
- });
Controller.html- <!DOCTYPEhtml>
- <htmlng-app="myApp">
-
- <head>
- <title>Controllers in AngularJS</title>
- <scriptsrc="Scripts/angular.min.js">
- </script>
- <scriptsrc="Scripts/controller.js">
- </script>
- </head>
-
- <body>
- <divng-controller="LaptopController">
- <h1>Learn Controller in AngularJS at C# Corner!</h1>
- <inputtype="button" ng-click="dell()" value="Dell" />
- <inputtype="button" ng-click="sony()" value="Sony" />
- <span><h2>{{laptop}}</h2></span>
- </div>
- </body>
-
- </html>
The following is the output when we run above code:
After clicking on
Dell button the value changes as in the following output:
The above output is in debugging mode with the help of Chrome’s Developer Tool we opened, and set breakpoint under the controller method ‘dell()’. Next, we click on
Dell button it will call the controller ‘dell()’ method and set the value to property ‘laptop’. Now the output will be like the following,
Same as when we click on Sony button you can see the following two output, first when we press Sony button and it calls the ‘sony()’ method in controller:
After continuing the debugging output is as follows:
Great, today we learned creating Controller in AngularJS with example created successfully.
Summary
I hope that beginners as well as students understood the concept of Controller in AngularJS with example. If you have any suggestion regarding this article, then please contact me. Stay tuned for other concepts of AngularJS coming soon.
Thanks for reading. Learn it! Share it.