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.
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.



Join the conversation! Your thoughts help the community grow.