1

I am very new in angular js. I am confused what is the 2 way data binding in angularjs. Suppose In my mvc4 application i have bind the model with the view and user changes the value of a textbox which is binded with a model class property.

After clicking the update button, model is passed to the controller with the changed value.Then the value is passed to the database to update the database. i.e. Model is updating with new values.

In my angularjs application I have also binded the same textbox with the model. To update the model user puts a new value to the textbox and model changes. The new model is passed through the web api controller to update the database.

My QUESTION is : What is the difference between this two type of binding mechanism.In both cases view and model is updated. So what is 2 way binding in angularjs. Please help me to clear this concept.

3 Answers 3

5

Two way binding in AngularJS is the synchronization between the view and model (without any need to refresh the page or click a button). Any change in the model is reflected on the view and any change in the view is reflected on the model. Thus, this way of two way binding ensures that your view and model are always updated. Also the controller can remain separate from the view and focus on the model.

This is how you can use two-way binding in AngularJS:

<div ng-app="myApp" ng-controller="myCtrl">
    Name: <input ng-model="name">
    <h1>{{name}}</h1>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.name = "HelloWorld";
});
</script>

MVC makes use of the ASP.NET model binder. It maps the values from an HTML page to a model. When MVC receives an HTTP request, it routes it to a specific action method of a controller. It determines which action method to run based on what is in the route data, then it binds values from the HTTP request to that action method’s parameters. AngularJS handles this differently since the controller is specified in the <div> using the ng-controller tag. Also, you don't need to return a view, the model synchronizes automatically with the view associated in the controller (without the need of return).

You can find some more information on the ASP.NET model binding here: https://docs.asp.net/en/latest/mvc/models/model-binding.html

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks.It is now clear.But when we bind model in mvc4 also the model changes when we change the view bind with the model.
@amitabha I've added a link for the ASP.NET model binder, hope it helps.
1

Two way DataBinding keeps the model and view in sync at all the times,that is change in the model updates the view and a change in the view updates the model.

  • Binding expression updates the view when the model changes.
  • ng-model directive updates the model when the view changes.

Comments

0

Data Binding in AngularJs is two ways: One is with Expression and another is with ngModel. Example: <div> Your Name is <input type="text" ng-model="name" /> My name is {{name}} </div>

Here ng-model="name" means we have defined name as angular variable with ng-model directive which will set the value of textbox to name variable and being displayed with {{name}} expression. This is the power of AngularJS

1 Comment

I understand this.But As per document "Two way data-binding is a very simple concept which provides synchronisation between Model and View layers. Model changes propagate to the View, and View changes are instantly reflected back to the Model. This makes the Model the "single source of truth" for maintaining the applications state." The same thing works in mvc4 also.So my question is what is difference?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.