Geeks With Blogs
Zakaria's Blog
Data Binding
Data-binding in Angular apps is the automatic synchronization of data between the model and view components. The way that Angular implements data-binding lets you treat the model as the single-source-of-truth in your application. The view is a projection of the model at all times. When the model changes, the view reflects the change, and vice versa.
Data Binding in Classical Template Systems
Most templating systems bind data in only one direction: they merge template and model components together into a view. After the merge occurs, changes to the model or related sections of the view are NOT automatically reflected in the view. Worse, any changes that the user makes to the view are not reflected in the model. This means that the developer has to write code that constantly syncs the view with the model and the model with the view.
Data Binding in Angular Templates
Angular templates work differently. First the template (which is the uncompiled HTML along with any additional markup or directives) is compiled on the browser. The compilation step produces a live view. Any changes to the view are immediately reflected in the model, and any changes in the model are propagated to the view. The model is the single-source-of-truth for the application state, greatly simplifying the programming model for the developer. You can think of the view as simply an instant projection of your model.
Because the view is just a projection of the model, the controller is completely separated from the view and unaware of it. This makes testing a snap because it is easy to test your controller in isolation without the view and the related DOM/browser dependency.
Example :
We’ll start by setting up a div with an “ng-app” attribute
index.html code snippet :
 <!DOCTYPE html>  
   <title>AngularJS Tutorials</title>  
   <link rel="stylesheet" href="vendor/foundation.min.css">  
   <div ng-app="">  
   <script type="text/javascript" src="vendor/angular.js"></script>  
This attribute says that this element (the div) and everything inside of it belongs to this app. Binding can be represented in AngularJS using the ng-bind directive or with double curly brackets. We can test this out by trying some expressions in our div:
 <div ng-app="">  
  {{1 + 1}}  
  {{"john" + "lindquist"}}  
  {{3 * 3}}  
You should keep logic in the view to a minimum. Binding is most useful when you create an input or a way for the user to interact with the site of your app. Let’s create an input with an attribute ng-model which is set to “data.message” and then bind it inside our div.
 <div ng-app="">  
  <input type="text" ng-model="data.message">  
Now when we load the page, if we start typing in the input field, you will see that the model has been bound and our text will start showing up as we type. If we add ‘+ “ world”’ into the binding we get “hello world” when we refresh and type hello into the input.
 <div ng-app="">  
  <input type="text" ng-model="data.message">  
  <h1>{{data.message + " world"}}</h1>  
We can get a bit crazy and create a div with a class attribute of “{{data.message}}” and say “Wrap me with a foundation component”

 <div ng-app="">  
  <input type="text" ng-model="data.message">  
  <h1>{{data.message + " world"}}</h1>  
  <div class="{{data.message}}">  
   Wrap me in a foundation component  
Now we can type foundation classes into the input box, such as “panel” “alert-box” and “label” we can see that it actually changes the class of the div and the div gets styled differently so we can really manipulate a lot of the dom using binding, not just presenting simple text.

Posted on Wednesday, June 4, 2014 7:33 PM Angular js | Back to top

Comments on this post: Angular js Basics-Data Binding

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © A.K.M. Zakaria | Powered by: