0

I have that question, I am looking for examples everywhere and all I see are those examples of adding inputs with ng-repeat, but I don't want that, all I want is to have a button which says add another input and once the user click on it, have a new input text below the existing one.

<div>
  <div>
    <input ng-model="operation.detailText" type="text">
    <div ng-show="operation.detailText.length">
      <p ng-show="operation.detailText.length">{{operation.detailText}}</p>
  </div>
</div>
7
  • Whats the problem with ng-repeat? Commented May 28, 2015 at 17:31
  • He doesn't want to add inputs in a loop, he wants to have a button that on click, it will add another input bellow the last added input. I believe he wants to do a dynamic form. Commented May 28, 2015 at 17:32
  • ng-repeat really is the way to go on this one. How else will you determine where to store the data from the input? Commented May 28, 2015 at 17:33
  • @taxicala. It can still be achived by ng-repeat Commented May 28, 2015 at 17:34
  • @Zee so how I do it with ng-repeat ? Commented May 28, 2015 at 17:34

3 Answers 3

3

A quick example:

HTML :

<div ng-controller="MyCtrl">
    <li ng-repeat="item in items">
        <input/>
    </li>
    <button ng-click="add()">Add</button>
</div>

JS:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.items = [1];
    $scope.add=function(){
      $scope.items.push($scope.items.length)
    }

}

Example fiddle

This is one way to do it. I simply add elements to the items which increases its length thereby adding one more input to it.

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

5 Comments

how I do it taking into account that the ng-model is operation.detailText
@NietzscheProgrammer. Different inputs cannot have same ng model, however you can have an array for the ng-models.
@NietzscheProgrammer. Every input will have its own ng-model, as you press the add button you need to add a new ng-model to the newly created input.
@NietzscheProgrammer. The above code is just an example, you can modify it as per your requirement :)
Take a look at my answer. It accounts for ng-model.
1

Here's what you need. You can do this with either ng-repeat or ng-repeat-start/ng-repeat-end (the latter results in fewer html elements, but for simplicity I'll just use ng-repeat here):

<div>
  <div ng-repeat="operation in operations">
    <input ng-model="operation.detailText" type="text"></input>
    <div>
      <p>{{operation.detailText}}</p>
    </div>
  </div>
  <button ng-click="operations.push({})">Add one more</button>
</div>

Note: operations here is an array, and it can be empty to start.

4 Comments

ok this way is working now, but if I click on that button, I am adding double inputs in another ng-model reproduced by a ng-repeat. Look at this video, is with your implementation, and see this question
You can't use the same array for both ng-repeats. You could add a filter to the ng-repeat ng-repeat="operation in operations | someOperationFilter
Be clear in your code about the concepts you're using (i.e. operations vs operationTypes) and don't mix them into the same array like that. jsbin.com/hujerurivu/1/edit?html,css,js,output
add that answer here stackoverflow.com/questions/30517983/… actually works as I want. I can give you better answer
1

If your list of inputs will grow indefinitely the best option is, indeed, the ng-repeat, but if all you need is another, predefined, input, you can use an ng-if to add the markup.
Something like the following:

<div ng-if="inputAdded">
    <!-- input and other stuff in here -->
<div>

<button ng-click="inputAdded = true"></button>

2 Comments

how should I do it with ng-repeat then ?
Take a look at Zee's answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.