1

I want to expand a basic angularjs form so that it can have a dynamic number of inputs. Basically I want to create a user that can have 1 or more siblings. If you click a + button on the form it will add an additional sibling input to the form and keep them as an array. What concepts would I use to accomplish this in angular?

How can I add elements to a partial view in angular when the user clicks? And how can I make those inputs be kept as an array?

<div ng-controller="Controller">
  <form novalidate class="simple-form">
    Name: <input type="text" ng-model="user.name" /><br />
    Siblings: <input type="text" ng-model="user.sibling[0]" /><br />
     <a href="#" name="moresiblings">+</a>
  </form>
  <pre>form = {{user | json}}</pre>
</div>

2 Answers 2

3

Wrap the siblings in ng-repeat

<span ng-repeat="sibling in user.siblings"><input type="text" ng-model="sibling" /></span>

Then when you add a new one push it to the user.sblings array in scope

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

Comments

2

I would suggest using ng-repeat on the sibling inputs, and attaching an ng-click function to your moresiblings link that pushes new siblings into the user.siblings array. Here is a fiddle that demonstrates the concept.

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.