Here I have taken three textboxes and bound these values at run time with the help of AngularJS. AngularJS is a powerful programming framework based on JavaScript provided by Google.
AngularJS extends HTML with new attributes. This is perfect for Single Page Applications.
What should you already know before learning the AngularJS:
- HTML
- CSS
- JavaScript(Master of web programming)
AngularJS is spread as JavaScript file and add with Script tag such as :
- <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
AngularJS extends HTML with ng-directives.
- ng-app directive define an AngularJS application.
- ng-model directive define an AngularJS model and bind the value of HTML control to application data.
- ng-bind directives binds application data to HTML view.
Now I have shown an example to bind data with HTML using AngularJS.
I have taken a page named AngularJsTest.html andthe page looks like below,
- <!DOCTYPE html>
- <html>
-
- <head></head>
-
- <body>
-
- <div>
- </div>
-
- </body>
-
- </html>
Above is the blank HTML page and now we have added HTML control,
- <form id="form1">
-
- <div>
- Name:<input type="text">
- <br>
- <br/>
- Email:<input type="text">
- <br>
- <br>
- Address:<input type="text">
- <br>
- <br>
-
- </div>
-
- </form>
Now add script for accessing AngularJS.
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Angular JS Test</title>
- <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
- </head>
- <body>
- <form id="form1">
- <div>
- Name:<input type="text">
- <br>
- <br/>
- Email:<input type="text">
- <br>
- <br>
- Address:<input type="text">
- <br>
-
- </div>
- </form>
- </body>
- </html>
We have added AngularJS script source to access the library and now we write model for data,
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Angular JS Test</title>
- <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
- <script>
- var app = angular.module('myApp', []);
- app.controller('personCtrl', function ($scope) {
- $scope.Name = "UpendraPratapShahi";
- $scope.Email = "upendra.pratap@****.com";
- $scope.Address = "Noida, India";
- $scope.EMP_Info = function () {
- return$scope.Name + " , " + $scope.Email + " , " + $scope.Address;
- }
- });
-
- </script>
- </head>
- <body>
- <form id="form1">
- <div>
- Name:<input type="text">
- <br>
- <br/>
- Email:<input type="text">
- <br>
- <br>
- Address:<input type="text">
- <br>
-
- </div>
- </form>
- </body>
- </html>
In the above I have defined a variable with name ‘app’ and give module name as ‘myApp’. Then created controller as ‘PersonCtrl’ and created a function for binding textbox values with given value and returned all and showed it on HTML page.
Now add define directives to the HTML controls,
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head>
- <title>Angular JS Test</title>
- <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
- <script>
- var app = angular.module('myApp', []);
- app.controller('personCtrl', function($scope)
- {
- $scope.Name = "UpendraPratapShahi";
- $scope.Email = "upendra.pratap@****.com";
- $scope.Address = "Noida, India";
- $scope.EMP_Info = function()
- {
- return$scope.Name + " , " + $scope.Email + " , " + $scope.Address;
- }
- });
- </script>
- </head>
-
- <body>
- <form id="form1">
- <div ng-app="myApp" ng-controller="personCtrl">
- Name:
- <input type="text" ng-model="Name">
- <br>
- <br/> Email:
- <input type="text" ng-model="Email">
- <br>
- <br> Address:
- <input type="text" ng-model="Address">
- <br>
- <br>
- <b>Employee Information :</b> {{EMP_Info()}}
- </div>
- </form>
- </body>
-
- </html>
In above HTML, I have bound angular model with controller, and now I will check on web browser.
Now render on browser as in the following image,
Now I have made changes in value on browser and see what happens,
Above was some information regarding AngularJS. I hope all you have enjoyed article and I welcome your feedback.
Read more articles on AngularJS: