Two-way data bindings tracks changes in both directions, allowing elements that gather data from the user to modify the state of the application. Two-way bindings are created with the ng-model directive, and a single data model property can be used for both one- and two-way bindings. For demonstrating this concept, below code works great:
TwoWayBind.html
- <!DOCTYPE html>
- <html ng-app="TestApp">
- <head>
- <title>AngularJS Two Way Binding</title>
- <script src="angular.js"></script>
- <link href="../../RefStyle/bootstrap.min.css" rel="stylesheet" />
- <script src="app.js"></script>
- <script src="TwoWayBind.js"></script>
- </head>
- <body ng-controller="TwoWayCtrl">
- <div id="todoPanel" class="panel">
- <h3 class="panel-header">To Do List</h3>
- <div class="well">
- <div>The first item is: {{todos[0].action}}</div>
- </div>
- <div class="form-group well">
- <label for="firstItem">Set First Item:</label>
- <input name="firstItem" class="form-control" ng-model="todos[0].action" />
- </div>
- </div>
- </body>
- </html>
TwoWayBind.js
- testApp.controller("TwoWayCtrl", function ($scope) {
- $scope.todos = [
- { action: "Get groceries", complete: false },
- { action: "Call plumber", complete: false },
- { action: "Buy running shoes", complete: true },
- { action: "Buy flowers", complete: false },
- { action: "Call family", complete: false }];
- });
The output of the above example is,
Using the Template Directives
Data bindings are the core feature of AngularJS views, but on their own they are pretty limited. Web applications—or any kind of application for that matter—tend to operate on collections of similar data objects and vary the view they present to the user based on different data values. Fortunately, AngularJS includes a set of directives that can be used to generate HTML elements using templates, making it easy to work with data collections and to add basic logic to a template that responds to the state of the data. I have summarized the template directives as below –
Directive | Applied | As Description |
ng-cloak | Attribute, class | Applies a CSS style that hides inline binding expressions, which can be briefly visible when the document first loads |
ng-include | Element, attribute, class | Loads, processes, and inserts a fragment of HTML into the Document Object Model |
ng-repeat | Attribute, class | Generates new copies of a single element and its contents for each object in an array or property on an object |
ng-switch | Element, attribute | Changes the elements in the Document Object Model based on the value of data bindings |
ng-if | Attribute | Adds and removes elements from the DOM |
ng-class | Attribute, class | Sets the class attribute for an element |
ng-class-even | Attribute, class | Sets the class attribute for even-numbered elements generated within the ng-repeat directive |
ng-class-odd | Attribute, class | Sets the class attribute for odd-numbered elements generated within the ng-repeat directive |
ng-hide | Attribute, class | Shows and hides elements in the DOM |
ng-show | Attribute, class | Shows and hides elements in the DOM |
ng-style | Attribute, class | Sets one or more CSS properties |
Demonstrate of ng-repeat
Html file code
- <!DOCTYPE html>
- <html ng-app="TestApp">
- <head>
- <title>AngularJS Ng Class</title>
- <script src="angular.js"></script>
- <link href="../../RefStyle/bootstrap.min.css" rel="stylesheet" />
- <script src="app.js"></script>
- <script src="OtherBind.js"></script>
- <style>
- .odd {
- background-color: lightcoral;
- }
-
- .even {
- background-color: lavenderblush;
- }
- </style>
- </head>
- <body ng-controller="OtherBindCtrl">
- <div id="todoPanel" class="panel">
- <h3 class="panel-header">To Do List</h3>
- <table class="table">
- <thead>
- <tr>
- <th>Srl No</th>
- <th>Action</th>
- <th>Done</th>
- </tr>
- </thead>
- <tbody>
- <tr ng-repeat="item in todos" ng-class="$odd ? 'odd' : 'even'">
- <td>{{$index + 1}}</td>
- <td>{{item.action}}</td>
- <td>{{item.complete}}</td>
- </tr>
- </tbody>
- </table>
- </div>
- </body>
- </html>
Angular Js Controller file
- testApp.controller("OtherBindCtrl", function ($scope) {
- $scope.todos = [
- { action: "Get groceries", complete: false },
- { action: "Call plumber", complete: false },
- { action: "Buy running shoes", complete: true },
- { action: "Buy flowers", complete: false },
- { action: "Call family", complete: false }];
- });
The output of the above program is as below,
Demonstrate of ngInclude
Main html file code
- <!DOCTYPE html>
- <html ng-app="TestApp">
- <head>
- <title>AngularJS ngInclude</title>
- <script src="angular.js"></script>
- <link href="../../RefStyle/bootstrap.min.css" rel="stylesheet" />
- <script src="app.js"></script>
- <script src="OtherBind2.js"></script>
- </head>
- <body ng-controller="OtherBindCtrl">
- <div id="todoPanel" class="panel">
- <h3 class="panel-header">To Do List</h3>
- <div class="well">
- <div class="checkbox">
- <label>
- <input type="checkbox" ng-model="showList">
- Use the list view
- </label>
- </div>
- </div>
- <ng-include src="viewFile()"></ng-include>
- </div>
- </body>
- </html>
AngularJs Controller file
- testApp.controller("OtherBindCtrl", function ($scope) {
- $scope.todos = [
- { action: "Get groceries", complete: false },
- { action: "Call plumber", complete: false },
- { action: "Buy running shoes", complete: true },
- { action: "Buy flowers", complete: false },
- { action: "Call family", complete: false }];
-
- $scope.viewFile = function () {
- return $scope.showList ? "list.html" : "table.html";
- };
- });
Now, as per the above code when view file method executes, it either returns list.html or table.html file as return value. For this, we need to create first this html file.
list.html
- <ol>
- <li ng-repeat="item in todos">
- {{item.action}}
- <span ng-if="item.complete"> (Done)</span>
- </li>
- </ol>
Table.html
- <table class="table">
- <thead>
- <tr>
- <th>#</th>
- <th>Action</th>
- <th>Done</th>
- </tr>
- </thead>
- <tr ng-repeat="item in todos" ng-class="$odd ? 'odd' : 'even'">
- <td>{{$index + 1}}</td>
- <td ng-repeat="prop in item">{{prop}}</td>
- </tr>
- </table>
The output of the program as follows,
Demonstrate the ngSwitch
Html file code
- <!DOCTYPE html>
- <html ng-app="TestApp">
- <head>
- <title>AngularJS ngSwitch</title>
- <script src="angular.js"></script>
- <link href="../../RefStyle/bootstrap.min.css" rel="stylesheet" />
- <script src="app.js"></script>
- <script src="OtherBind3.js"></script>
- </head>
- <body ng-controller="OtherBindCtrl">
- <div id="todoPanel" class="panel">
- <h3 class="panel-header">To Do List</h3>
- <div class="well">
- <div class="radio" ng-repeat="button in ['None', 'Table', 'List']">
- <label>
- <input type="radio" ng-model="data.mode" value="{{button}}" ng-checked="$first" />
- {{button}}
- </label>
- </div>
- </div>
- <div ng-switch on="data.mode">
- <div ng-switch-when="Table">
- <table class="table">
- <thead>
- <tr><th>#</th><th>Action</th><th>Done</th></tr>
- </thead>
- <tr ng-repeat="item in todos" ng-class="$odd ? 'odd' : 'even'">
- <td>{{$index + 1}}</td>
- <td ng-repeat="prop in item">{{prop}}</td>
- </tr>
- </table>
- </div>
- <div ng-switch-when="List">
- <ol>
- <li ng-repeat="item in todos">
- {{item.action}}<span ng-if="item.complete"> (Done)</span>
- </li>
- </ol>
- </div>
- <div ng-switch-default>
- Select another option to display a layout
- </div>
- </div>
- </div>
- </body>
- </html>
Angular Js File
- testApp.controller("OtherBindCtrl", function ($scope) {
-
- $scope.data = {};
-
- $scope.todos = [
- { action: "Get groceries", complete: false },
- { action: "Call plumber", complete: false },
- { action: "Buy running shoes", complete: true },
- { action: "Buy flowers", complete: false },
- { action: "Call family", complete: false }];
-
- $scope.viewFile = function () {
- return $scope.showList ? "list.html" : "table.html";
- };
- });
The output of the above code as below,
Demonstrate the ngClass and ngStyle
Html file code
- <!DOCTYPE html>
- <html ng-app="TestApp">
- <head>
- <title>AngularJS ngClass and ngStyle</title>
- <script src="angular.js"></script>
- <link href="../../RefStyle/bootstrap.min.css" rel="stylesheet" />
- <script src="app.js"></script>
- <script src="OtherBind4.js"></script>
- <style>
- tr.Red {
- background-color: lightcoral;
- }
-
- tr.Green {
- background-color: lightgreen;
- }
-
- tr.Blue {
- background-color: lightblue;
- }
- </style>
- </head>
- <body ng-controller="OtherBindCtrl">
- <div id="todoPanel" class="panel">
- <h3 class="panel-header">To Do List</h3>
- <div class="row well">
- <div class="col-xs-6" ng-repeat="(key, val) in settings">
- <h4>{{key}}</h4>
- <div class="radio" ng-repeat="button in buttonNames">
- <label>
- <input type="radio" ng-model="settings[key]"
- value="{{button}}">{{button}}
- </label>
- </div>
- </div>
- </div>
- <table class="table">
- <thead>
- <tr><th>#</th><th>Action</th><th>Done</th></tr>
- </thead>
- <tr ng-repeat="item in todos" ng-class="settings.Rows">
- <td>{{$index + 1}}</td>
- <td>{{item.action}}</td>
- <td ng-style="{'background-color': settings.Columns}">
- {{item.complete}}
- </td>
- </tr>
- </table>
- </div>
- </body>
- </html>
Angular JS code
- testApp.controller("OtherBindCtrl", function ($scope) {
-
- $scope.todos = [
- { action: "Get groceries", complete: false },
- { action: "Call plumber", complete: false },
- { action: "Buy running shoes", complete: true },
- { action: "Buy flowers", complete: false },
- { action: "Call family", complete: false }];
-
- $scope.buttonNames = ["Red", "Green", "Blue"];
-
- $scope.settings = {
- Rows: "Red",
- Columns: "Green"
- };
- });
The output of the above code is as below,
Demonstrate how to bind html form with angular js
Html file code
- <!DOCTYPE html>
- <html ng-app="TestApp">
- <head>
- <title>AngularJS Forms</title>
- <script src="angular.js"></script>
- <link href="../../RefStyle/bootstrap.min.css" rel="stylesheet" />
- <script src="app.js"></script>
- <script src="Forms.js"></script>
-
- </head>
- <body ng-controller="FormsCtrl">
- <div id="todoPanel" class="panel">
- <h3>
- To Do List -
- <span class="label label-info">
- {{ (todos | filter: {complete: 'false'}).length}}
- </span>
- </h3>
- <br />
- <div class="row">
- <div class="col-xs-6">
- <div class="well">
- <div class="form-group row">
- <label for="actionText">Action:</label>
- <input id="actionText" class="form-control" ng-model="newTodo.action">
- </div>
- <div class="form-group row">
- <label for="actionLocation">Location:</label>
- <select id="actionLocation" class="form-control" ng-model="newTodo.location">
- <option>Home</option>
- <option>Office</option>
- <option>Mall</option>
- </select>
- </div>
- <button class="btn btn-primary btn-block"
- ng-click="addNewItem(newTodo)">
- Add
- </button>
- </div>
- </div>
- <div class="col-xs-6">
- <table class="table">
- <thead>
- <tr><th>#</th><th>Action</th><th>Done</th></tr>
- </thead>
- <tr ng-repeat="item in todos">
- <td>{{$index + 1}}</td>
- <td>{{item.action}}</td>
- <td>
- <input type="checkbox" ng-model="item.complete">
- </td>
- </tr>
- </table>
- </div>
- </div>
- </div>
- </body>
- </html>
Angular Js Code
- testApp.controller("FormsCtrl", function ($scope) {
-
- $scope.todos = [
- { action: "Get groceries", complete: false },
- { action: "Call plumber", complete: false },
- { action: "Buy running shoes", complete: true },
- { action: "Buy flowers", complete: false },
- { action: "Call family", complete: false }];
-
- $scope.addNewItem = function (newItem) {
- $scope.todos.push({
- action: newItem.action + " (" + newItem.location + ")",
- complete: false
- });
- };
- });
The output of the program is as below,