12

I'm working on an Angular application.

I want to generate a form with an arbitrary number of text input fields with two-way bindings for every individual input field. No buttons, no watchers. ng-model is not working correctly because of the scoping (if I'm not mistaken). The input fields are generated from an array with ng-repeat like this:

 <div ng-repeat="item in items">
   <label>{{item.name}}</label>
   <input type="text" placeholder="{{item.default}}" ng-model="{{item.value}}"> <!-- this input should be bound -->
 </div>

I just want a simple binding to update the items array in the controller on changes in the input.

Any help appreciated.

2
  • 3
    You only need angular braces {{var}} if you're writing it out to the page. When using it in repeats or models or filters, you don't need the braces. Note you're already doing this in the ng-repeat, you didn't do "item in {{items}}". Commented Feb 21, 2013 at 19:41
  • Thank you for the advice James! I think I understood Angular a little bit better (I'm still new to it though :) ) Commented Feb 21, 2013 at 21:50

1 Answer 1

11

Just change input tag so it reads:

<input type="text" placeholder="{{item.default}}" ng-model="item.value">

Notice ng-model without curly braces.

Working plunk: http://plnkr.co/edit/CLdem9yIw2Sk1U52Iajl?p=preview

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

1 Comment

this plnkr does not work. I clearly see the mustache value {{item.name}} is not getting updated.

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.