Two-way data binding and templates

Two-way data binding is one of the most interesting parts of AngularJS. It is responsible for synchronizing changes made to Model, and it manipulates DOM instantly. Without their help, it would need to write many, many lines of code to monitor changes to Model and return them to update DOM.

Let's see the following code, which illustrates the data binding:

<div ng-controller="UserController">
  <h1>{{ user.name }} - {{ user.age }}</h1>
  <p ng-bind="user.name"></p>
  <p ng-bind="user.age"></p>
  <br>
  <input type="text" ng-model="user.name">
  <input type="text" ng-model="user.age">
</div>

Just below the <h1> tag, where we used the expression with double brackets, we can see two paragraphs with the ng-bind directive; it does the same thing as the expression, but if the code for some reason take a few seconds to render, the user can view the double brackets before AngularJS takes control of the page.

Reusing templates

As we can see in the previous example, AngularJS allows us to create templates; HTML blocks with expressions and directives to manipulate DOM. We can reuse templates with the following code:

<div ng-controller="BandController">
  <ul>
    <li ng-repeat="band in bands">{{ band.name }} - {{ band.album}}</li>
  </ul>
</div>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.183.210