2

I am a newbie trying to understand angular two way data binding. In the docs https://docs.angularjs.org/guide/databinding, it mentions "Any changes to the view are immediately reflected in the model, and any changes in the model are propagated to the view". So if there is an input box ( model ) and an expression ( view ) I can see that "and any changes in the model are propagated to the view" works, but I want to see an example of the opposite scenario, ie.,"Any changes to the view are immediately reflected in the model", and how could I demonstrate it my self. Any help would be highly appriciated. Thanks

4 Answers 4

2

Two-way binding just means that:

  1. When properties in the model get updated, so does the UI.
  2. When UI elements get updated, the changes get propagated back to the model.

Also more

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

Comments

1

Refer this working code JsFiddle

$watch will watch the variable (ng-model) for any changes in this case and invokes the function. Even without adding $watch, whatever you type in the input box gets updated in the backend automatically. $watch is one way to check if the model is updated properly.

On clicking Login button the latest model value will be present in the controller.

$scope.$watch('user.firstName',function(){
        alert("Your name is changed to : "+$scope.user.firstName);
    });

Comments

0

In this basic example of angularjs we are using a directive ng-model. It has two-way data binding ($scope --> view and view --> $scope).$scope is an object which maintains an array $$watchers for each object bound to it and each object in this array has a key 'last' which stores the last updated value in the model.

<div ng-app>
  <div>
    <label>Name:</label>
    <input type="text" ng-model="yourName" placeholder="Enter a name here">
    <hr>
    <h1>Hello {{yourName}}!</h1>
  </div>
</div>

In this case "yourName" model is bound with $scope. So,angularjs internally looks up for change in this model using $watch and the digest cycle updated all the changes immediately to the view

 $watch(watchExpression, listener, [objectEquality]);//Registers a listener callback to be executed whenever the watchExpression changes.
 $digest();//Processes all of the watchers of the current scope and its children.

You can also look up for the change in the model in your angular controller as

$scope.$watch('yourName',function(){
    //do your stuff when the model changes
}); 

You'll see that if you change the view ie your inputbox,the model defined in ng-model is changed and this changed model reflects back to the view.

4 Comments

I can be wrong, But what I think/thought is , {{yourName}} is view and the input box itself is model source. But you say - input box as view - " if you change the view ie your inputbox,the model defined in ng-model is changed thus the two binding takes place ". So even if input box is view what is the model in ng-model ? Sorry I am still confused.
It is a MVC architecture(Model View Controller).So,Model and View are two different things.In the above scenario,input box is the view,and It is bound to a model.Model is an object which stores data.
Understood. So the input field is view and nd-model's variable is the model which is monitored by scope. Thanks
Hope it solves your problem.Please vote up and accept my answer
0

Two way data binding in angular:

It helps the user to pass the data from the component to view and from view to the component. so this will establish "bi-directional communication".

This can be achieved via [(ngModel)] and also known as 'banana-box syntax', refer it below for snippet to use:

import { Component } from '@angular/core'; 
 @Component({ 
   selector: 'app-example', 
   template: ` 
               Enter the value  : <input [(ngModel)] ='val'> 
               <br> 
                Entered value is:  {{val}} 
             ` 
}) 
export class AppComponent { 
   val: string = ''; 
}

Import FormsModule into app.module.ts to avoid Template parse error while using ngModel:

    import { FormsModule } from "@angular/forms"; 
    import { BrowserModule } from '@angular/platform-browser'; 
    import { AppComponent } from './app.component'; 
     @NgModule({ 
       imports: [BrowserModule, FormsModule], 
       declarations: [ AppComponent], 
       bootstrap: [AppComponent] 
    }) 

export class AppModule { }

I Hope it's clear.

Comments

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.