2

I have a template. In the template, there are three boxes. Each box has add button, which will add an input field in that box. I have tried the following codes.

HTML

<div class="row">
        <div ng-repeat="item in items2">
          <div class="theBox">
            <button class="btn btn-success" ng-click="addItem()">add</button>
            <h6>{{item.key}}</h6>

            <input ng-model="item.value">
          </div>
        </div>

</div>

AngularJs

 $scope.items2 = [
      {
        "key" : "a",
        "value" : []
      },
      {
        "key" : "b",
        "value" : []
      },
      {
        "key" : "c",
        "value" : []
      }
  ];


  $scope.addItem = function(){
    console.log("clicked ")
   angular.forEach($scope.items2, function(mm){
     mm.value.push({})
   })

  }

The Problem: If I click on add, it's creating an object inside each value, maybe because I have used the forEach-loop for each value. How can I add it individually?

Also The input tag is not added as well, how can I fix that ?

2 Answers 2

2

Your question it's not exactly clear.

If you are going to

add an input field in that box

every time you click on the add button, then you will need another ng-repeat in your HTML. Something like:

<div class="row">
    <div ng-repeat="item in items2">
      <div class="theBox">
        <button class="btn btn-success" ng-click="addItem(item)">add</button>
        <h6>{{item.key}}</h6>

        <input ng-repeat="itemValue in item.value" ng-model="itemValue.text">
      </div>
    </div>

and your javascript to:

$scope.addItem = function(item){ 
    item.value.push({text:""});
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is really close what i am expecting. however the controller is not taking the values from the input field once its created
Yeah, my bad. as @Maxim Shoustin suggested in his post, you need to create a "text" parameter for your input. i'll edit my post accordingly
2

which will add an input field in that box.

If I understand you right you want to add <input>s to specific box.

I would write something that:

<div class="row">
        <div ng-repeat="item in items2">
          <div class="theBox">
            <button class="btn btn-success" ng-click="addItem(item)">add</button>
            <h6>{{item.key}}</h6>
          <div ng-repeat="value in item.values">
             <input  ng-model="value.text">
            </div>    
          </div>
        </div>
</div>

and method will be:

 $scope.addItem = function(selectedItem){
   selectedItem.values.push({text:""});  
 }

Demo Plunker


Tips:

  • If you have list og inputs, the good practice to name it values and not value in { "key" : "a", "value" : [] },
  • input ng-model is based on string and not object so will be better to set {text:""} and <input ng-model="value.text">

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.