CodesJava

Easy learning with example program codes

Angularjs two way data binding tutorial


As we discussed in earlier tutorials that ng-model directive is used to define the model or variable for a HTML element to be used in AngularJS i.e. it binds the state of HTML element with model value. If the value HTML element changes, AngularJS automatically change the model value and vice-versa.

Angularjs two way data binding diagram:

two-waybinding codesjava
 

Example Explanation:

First include the AngularJS library in the application. The ng-app directive initializes the application. The ng-model directive binds the state of the input text to the name variable. When we enter a value in name input box, the corresponding value will automatically updated in next line and vice-versa.

Example:

<!DOCTYPE html>
<html>
 <script src=
  "http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">
 </script>
 <body>
   <div ng-app="">
    <p>Name: <input type="text" ng-model="name"></p>
     <h1>Hello {{name}}</h1>
    </div>
 </body>
</html>

Try it:

JS Bin on jsbin.com
 



Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap