Book Image

Learning Single-page Web Application Development

Book Image

Learning Single-page Web Application Development

Overview of this book

Table of Contents (15 chapters)
Learning Single-page Web Application Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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