Introduction
In this article, we are going to see the controller and also how to register the module with the controller. Two-way data binding is also explained in this article.
Controller
The controller is a javascript function. The controller is to build a model for a view to display. In a real-world application with our web service it is used to retrieve data from the database. Let us see an example.
- var myapp = angular.module("test", []);
- myapp.controller("sample", function($scope) {
- var student = {
- Rollno: "0001",
- Name: "navi",
- Age: "19"
- };
- $scope.student = student;
- });
The above code contains a module and a controller. The controller is registered with the module.
The module name is the test and the controller name is a sample. The module is registered. The HTML code is given below.
- <html ng-app="test">
-
- <head>
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
- <script src="C:\Users\Naveen\Desktop\navi.js"></script>
- </head>
-
- <body ng-controller="sample"> Rollno:{{student.Rollno}}<br> Name:{{student.Name}}<br> Age:{{student.Age}}<br> </body>
-
- </html>
By running this code, the output is given below
It displays the respective name and the age because in the code I link the module by using the ng-app (inside the HTML tag) and also I link the controller by using (inside the body tag).
ng-src
Using a binding expression with img src attribute results in a 404 error in Angular JS. The error is due to the DOM being parsed and an attempt is made to fetch the image from the server. But Angular JS binding expression that is specified with src attribute is not evaluated. To fix the error we can use ng-src directive. It assures us that the request is sent only after Angular JS has evaluated the binding expression.
Two-way data binding
This means that a change in the model updates the view and a change in the view update the model. Binding expression updates the view where the model changes .ng-model directives update the model when the view changes.
- <div ng-app="myApp" ng-controller="myCtrl"> Name: <input ng-model="message">
- <h1>{{message}}</h1>
- </div>
- Inside the js file var app = angular.module('sample', [ ]);
- app.controller('myCtrl', function($scope) { $ });
ng-repeat
ng-repeat is the same as for loop in C-sharp. ng- repeat is mostly used in the html tables The example for ng-repeat is given below:
- <html>
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
-
- <body ng-app="sample" ng-controller="test">
- <div ng-repeat="i in cities">{{i}} </div>
- <script>
- var app = angular.module("sample", []);
- app.controller("test", function($scope) {
- $scope.cities = ["Mumbai", "New Delhi", "Chennai", "Bangalore", ]
- });
- </script>
- </body>
-
- </html>
The output of the code is given below
The working of ng-repeat is the same as the for each loop. Suppose we want to find the index of each element -- it’s very easy .Just add ng-init in the above code.
- <div ng-repeat="i in cities" ng-init="parentIndex=$index">{{i}}+Index:{{$index}} </div>
- <script>
The output of the code is given below
Conclusion
In this article, we have learned about the two-way data binding in Angular JS. In my next article, we will see some other concepts in the Angular JS.