Data Binding in Angular JS

In Angular JS, one thing which makes it the easiest javascript framework to work with, is 2 way data binding.
So here is a simple example of how Data binding works, this post should be seen from the prism of a developer who has never much worked on front end development and just started to work on JavaScript.
Now-a-days ExtJS is a preferred choice to work on front end development, this seems to be a good replacement for frameworks like Adobe flex and GWT, as it provides good number of widgets for grids and graphs.
If we work with such frameworks you need to navigate through DOM and get to the point of interest to get value and then pass to javascript then process it as required. This is done away with in Angular JS, you update data(model) on the view and automatically the model in the controller reflects this change. This is achieved using the Observer pattern, listeners listen on this attribute in a loop and when they detect a change in value they call a function to replace the old value with the new one.
So below is the code showing the same in the view or the html file.
  1. <!Doctype html>
  2. <head>
  3. <script src="../bower_components/angular/angular.js"></script>
  4. <script src="../bower_components/jquery/dist/jquery.min.js"></script>
  5. <script src="../bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
  6. <script src="DataController.js"></script>
  7. <script src="ChildController.js"></script>
  8. <link rel="stylesheet" href="../bower_components/bootstrap/dist/css/bootstrap.min.css">
  9. <link rel="stylesheet" href="../bower_components/bootstrap/dist/css/bootstrap-theme.min.css">
  10. <link href='https://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet' type='text/css'>
  11. <link rel="stylesheet" href="../css/app.css">
  12. </head>
  13. <body ng-app="mainApp" ng-controller="IndexController">
  14. <div class="container">
  15. <div class="row" >
  16. <div class="col-xs-6 col-md-4 "><h3>Angular JS Lesson 1</h3>
  17. <ol>
  18. <h3><li>
  19. <a href="https://docs.angularjs.org/guide/databinding">Data Binding in Angular</a></li></h3>
  20. <h3><li>Models are “the single source of truth”.</li></h3>
  21. <h3><li>When model changes listeners are fired to replace the old value with the new value, aka Observer pattern.</li></h3>
  22. <h3><li>Check out $digest, $apply and $watch to understand how listeners, digester(dirty checking) and Updating ($apply) makes 2 way databinding works.</li></h3>
  23. </ol>
  24. </div><br/><br/>
  25. <div class="col-xs-6 col-md-4"></div>
  26. <div class="col-xs-6 col-md-4 barc">
  27. <h3 class="success">Data Binding Fundamentals</h3>
  28. <input type="text" class="form-control" data-ng-model="name" placeholder="Please Enter Your Name" />
  29. <h3 ng-if="name"> Welcome to Angular,{{ name }} you look like a {{animal}}</h3>
  30. <label>Main Controller</label>
  31. <h3 >Favorite Animal:</h3>
  32. <input type="text" class="form-control" data-ng-model="a.firstName" />
  33. <h3>{{ a.firstName }}</h3>
  34. <div data-ng-controller="ChildController">
  35. <label>Child Controller</label>
  36. <h3 >Favorite Animal:</h3>
  37. <input type="text" class="form-control" data-ng-model="a.firstName" />
  38. <h3>{{ a.firstName }}</h3>
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43. </body>
  44. </html>
The lines of interest to understand simple data binding are,
  1. <input type="text" class="form-control" data-ng-model="name" placeholder="Please Enter Your Name" />
  2. <h3 ng-if="name"> Welcome to Angular,{{ name }} you look like a {{animal}}</h3>
So model “name” is bounded to the input text and as you type in the text box its updated and displayed below real-time.
Similarly, if we define a controller ( remember the @controller of Spring but on the client side which has access to this data) and attach this model to $scope variable in the controller changes are reflected and updated on both the ends. Below is the controller,
  1. var app = angular.module("mainApp",[]);
  2. app.controller( "IndexController", function ( $scope ) {
  3. $scope.animal = "Snow Leopard";
  4. $scope.firstName = "Wolf";
  5. });
If the variable $scope.animal is updated in this controller or the view changes are reflected at both the places.
There is a catch here, if you have embedded controllers ( remember parent-child relationship in Java)  i.e. if we use a <div> controlled by One controller and a <div> inside parent <div> controlled by another controller. Even if we use same $scope variable in both the controller the updates are not synchronized between them.
We need to use a dot operator on the model name, this makes both the parent and child controller’s model to be in sync. Below is the child controller code.
  1. var app = angular.module("mainApp");
  2. app.controller( "ChildController", function ( $scope ) {
  3. $scope.firstName = "Lion";
  4. });
We you do not want the model from the parent and child controllers to be in sync just remove the dot(.) operator from the model name.
Css code for the some beautification of the view, i like to make the view look good as it helps me learn some CSS.
  1. h3 {
  2. color: #B4886B;
  3. font-weight: bold;
  4. display: block;
  5. width: 400px;
  6. float: left;
  7. font-family: 'Montserrat', sans-serif;
  8. }
  9. .barc {
  10. border-radius: 15px;
  11. color: navy;
  12. background-color: #D8CEF6;
  13. border: 2px solid #D8CEF6;
  14. padding: 5px;
  15. }
  16. label {
  17. color: #1491BA;
  18. font-weight: bold;
  19. display: block;
  20. width: 400px;
  21. float: left;
  22. font-family: 'Montserrat', sans-serif;
  23. }
The application looks like below:
DataBinding in Angular JS
DataBinding in Angular JS

Thats it, to learn and get in details check out $watch, $apply and $digest.

Comments

Popular posts from this blog

Java Interview : Threads

Spring Framework Interview Notes : Part Two Wiring

Card Dealer In Java in Less than 5 minutes