2

I am new to AngularJs, and hence i am curious about all the features it has to offer.

When i read about two way binding, I felt like experimenting it on 2 Textboxes. Where I would enter text into 1st Textbox and at the same type i can see the same text being reflected in the 2nd textbox. I searched the web for such examples of 2 way binding, but could only find examples of textbox and span.

So could anyone help me? this is what i tried

 <html ng-app="myapp">
    <div ng-bind="">
    <input type="text"  ng-model="name" >
    <input type="text" ng-bind="name" >
    </div>
    </html>

and also

 <input type="text"  ng-model="name" >
    <input type="text" ng-value="name" >

and

<input type="text"  ng-model="name" >
<input type="text" ng-value={{name}} >

But nothing seems to work.

1
  • 1
    Use the same ng-model for both Commented Mar 4, 2016 at 11:18

3 Answers 3

6

Two way binding means you can bind some value from html page to Angular controller. Hope this Plunker would help you understand it

<div ng-controller="mainCtrl">
  Val1: <input type="text"  ng-model="name" name="val1">
  <p>
  Val2: <input type="text"  ng-model="name" name="val2">
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

What is the use of $timeout parametre after the $scope parametre.?@shashank
@Shekar.gvr: My bad. It was there by mistake. ;) not relevant in this scenario
I have already figured out that it is irelevent in this scenario...but as i am new to angular, i feel curious with evryting new about it. so wouldn't mind an explaination on what $timout does. :p @Shashank
@Shekar.gvr: plnkr.co/edit/N3Rv34?p=preview to make u feel comfortable. Its similar to setTimeout of javascript. Try this stackoverflow.com/questions/19609796/…
0

You should use ng-model for both Textboxes-

<input type="text"  ng-model="name" >
<input type="text" ng-model="name" >

Comments

0

Adding to what @d-bro82 and @Shailendra Singh Deol posted

AngularJs supports MVVM framework which synchronizes the data automatically between model and view.

The ng-model directive binds the value of HTML controls like input, select, textarea.

In the first text box when you type some text, it binds to the model.

<input type="text"  ng-model="name" >

When you create a second textbox with same model, it reflects the value which is already binded.

<input type="text"  ng-model="name" >

1 Comment

But unfortunately that doesn't work. Why ? it would be fascinating to know

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.