Two Way Data Binding In Angular Without $scope Object Using MVC

Introduction
 
Data binding is the most useful and powerful feature among any of the existing or upcoming software development technologies. It is actually a process that bridges a connection between the view and business logic of the application.Basically, we will see one-way and two-way data binding with respect to AngularJS applications. But before we jump to that section, we will try to learn something about the scopes in AngularJS. 
 
Description
 
Here, I will show you how two-way binding in AngularJS MVC works without $Scope object.
 
To know more details
 
 
Source Code
 
 
One Way Data Binding In AngularJS

One-way data binding is an approach where a value is taken from the data model and inserted into an HTML element. There is no way to update model from view.

AngularJS provides some predefined data binding directives which are as follows:

  • ng-bind – Binds the inner Text property of an HTML element.
  • ng-bind-template - Almost similar to the ng-bind directive but allows for multiple templates.
  • ng-non-bindable - Declares a region of content for which data binding will be skipped.
  • ng-bind-html - Creates data bindings using the inner HTML property of an HTML element.
  • ng-model - Creates a two-way data binding.

Two Way Data Binding

In simple terms, two-way data binding is when the model changes, the view reflects the change and vice versa. Two-way bindings in AngularJS are created with the ng-model directive. Practically, two-way bindings can be applied only to those elements that allow the user to provide a data value, which means the input, textarea, and select elements.

Note
 
Scopes are those objects that contain functionality and the data that has to be used when rendering the View. The scopes of the application refer to the application model, so you can think of scopes as a View Model.Scopes are the source of truth for the application state. Because of this live binding, we can rely on the $Scopes to update immediately when the View modifies it, and we can rely on the View to update when the $scope changes. 

About $Scopes

  • Gives observers to watch for all the model changes.
  • Gives the ability to propagate model changes through the application as well as outside the system to other components associated.
  • Scopes can be nested in such a way that they can isolate functionality and model properties.
  • Gives an execution environment in which expressions are evaluated.

When the model changes the view is automatically updated. This is achieved using the data binding expression in the view.

AngularJS ng-bind and Html Span Element Templates
  • ng-bind
    This directive binds the model value to the HTML element.

  • Template
    Templates are wrapped within curly braces {{ }}. It is used to bind expressions to HTML.
Steps To  Two way Databinding  Using AngularJS without $scope object
 
Step-1
 
Create an MVC application named "Satyatwowaydbp1".
 
Code ref of  Controller name i.e. HomeController.cs,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace Satyatwowaydbp1.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         public ActionResult Index()  
  12.         {  
  13.             return View();  
  14.         }  
  15.     }  

Code Description
 
Here, Index is the Controller action method name.
 
Code ref of View name i.e. \Home\Index.cshtml,
  1. @{  
  2.     ViewBag.Title = "Satyaprakash";  
  3. }  
  4.   
  5. <style>  
  6.   
  7.     input[type=text], select {  
  8.         width: 60%;  
  9.         padding: 12px 20px;  
  10.         margin: 8px 0;  
  11.         display: inline-block;  
  12.         border: 1px solid #ccc;  
  13.         border-radius: 4px;  
  14.         box-sizing: border-box;  
  15.     }  
  16. </style>  
  17.   
  18. <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>  
  19.   
  20. <fieldset>  
  21.     <legend style="font-family:Arial Black;color:blue">Two Way DataBinding AngularJS IN MVC 4.5</legend>  
  22.   
  23.     <div ng-app="">  
  24.         My Full Name :  
  25.         <input type="text" ng-model="MyTitle" placeholder = "Enter Your Name Here....">  
  26.         <hr />  
  27.         My Name Is 1st Time: <span style="color:blue;background-color: Yellow;color: Blue;font-style: oblique;font-weight: bold">{{MyTitle}}</span>  
  28.         <br />  
  29.         <br/>  
  30.         My Name Is 2nd Time: <span style="color:orangered;background-color:yellow;font-style: oblique;font-weight: bold" ng-bind="MyTitle"></span>  
  31.     </div>  
  32.     </fieldset>  
  33.     <footer>  
  34.         <p style="background-color: Yellow; font-weight: bold; color:blue; text-align: center; font-style: oblique">© @DateTime.Now.ToLocalTime()</p> @*Add Date Time*@  
  35.     </footer>  
Code Description
 
To display the record using ng-bind and templates in AngularJS, in this example, I have set the ng-model directive for the TextBox.
  1. <input type="text" ng-model="MyTitle" placeholder = "Enter Your Name Here...."
There are two HTML SPAN elements of which one is set with a template expression while other is specified with the ng-bind directive.
  1. <hr />  
  2.        My Name Is 1st Time: <span style="color:blue;background-color: Yellow;color: Blue;font-style: oblique;font-weight: bold">{{MyTitle}}</span>  
  3.        <br />  
  4.        <br/>  
  5.        My Name Is 2nd Time: <span style="color:orangered;background-color:yellow;font-style: oblique;font-weight: bold" ng-bind="MyTitle"></span> 
These two names will show with different colors that will specify easily HTML SPAN element and ng-bind directive.
 
As we type in the TextBox, both the HTML SPAN elements display the value of the TextBox dynamically and that too without writing a single line of JavaScript code. Here no button click event also not required.

Advantages
 
Because of the two-way data binding provided by angular, as you type in the textbox, the value is immediately displayed in the View just below the textbox. This two-way binding feature provided by Angular eliminates the need to write any custom code to move data from the model to the View or from the View to the model. The Model and View are synchronized. In other words, when the Model data changes, the change is reflected in the View while when the View changes, the Model is updated.
 
OUTPUT
 
The URL Is: http://localhost:49906/Home/Index
 
 
 
GIF for better understanding.
 
 
MODULE SUMMARY
  • What is two-way binding in AngularJS.
  • Without $scope object performs two-way binding in AngularJS.
  • Advantages of AngularJS.
  • Difference between One-way and Two-way data binding.

Up Next
    Ebook Download
    View all
    Learn
    View all