Introduction
In this article, we will see controllers and the modules in AngularJS. And also, we will work with some examples. The topics we are going to discuss in this article are listed below.
- Angular CLI
- Angular expression
- Module
- Controller
- Examples
Angular CLI
It’s a command line interface that is used to scaffold and build Angular apps using NodeJS style modules. It’s easy to create an application that already works. The working with Angular CLI is known as generator programs.
Angular expression
Angular expressions are the same as JavaScript expressions as they contain literals, operators, and variables. Angular expressions are inside the double curly braces. They are known as head directives.
For example, the following code is used to add to two numbers.
- <!Doctype html>
- <html>
-
- <head>
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
- </head>
-
- <body>
- <div ng-app> Sum:{{15+30}} </div>
- <div> Sum:{{15+30}} </div>
-
- <body>
-
- </html>
After running this application, the output will be like below.
Sum:45
Sum:{{15+30}}
Because, in the first division, we included the ng-app directives that are manipulated by Angular whereas, in the second div, it is same as a normal statement. Suppose, we use the ng-app in the HTML tag, the output of the application will be like below.
Sum:45
Sum:45
JavaScript object
Now, let us work with AngularJS with the JavaScript object. The code is shown below.
- <div>
- {{ {name:’navi’,age:’23’}.age}}
- </div>
The output will be,
23
In this, consider that the ng-app is used with in the HTML tag.
Arrays
Now, while working with arrays, the arrays are same as in the JavaScript. Consider the following example.
- <div> {{ [‘Mumbai’,’Chennai’,’Delhi’][1]}} </div>
The output will be,
Chennai
Modules
Module is a controller for the different parts of our application, filter, directive etc. Modules are equivalent to the Main()method of an Angular application. Modules declaratively specify how the Angular application should be bootstrapped. The following steps are used to create a module in AngularJS.
It’s easy to create to create a module in AngularJS,
- Var sample=angular. Module(“test”,[]);
- Here, test is the module name
- [ ] is the dependent.
Module can be dependant on other modules too.
Controller
Controller is a JavaScript function. The controller is used to build a model for the view to display. The model is nothing but it’s a data. In a real world application, it is used to retrieve data from the database. To create a controller is easy. Just follow the steps given below.
- Var c = function($scope) {
- $scope.message = ”Welcome”;
- }
Attaching the message property to the Controller
Register the controller to the module
- Sample.controller(“c”, function($scope) {
- $scope.message = ”welcome”;
- });
Conclusion
In this article, we learned about Controllers and Modules in AngularJS. In my next article, we will learn some other topics in AngularJS.