1

I am a beginner in angularjs. I want to dynamically add a div while clicking on add icon.I have done some script on this .

HTML part:

<div ng-show="child" class="childDiv" data-ng-repeat="choice in choices track by $index">
    <label for="childname" class="childname"  >ServerName </label>
    <input  ng-model="serverinfo.childname" type="text" required="required"  placeholder="name"/>
    <label for="childname" class="childname"  >Fullname</label>
    <input  ng-model="serverinfo.childfullname" type="text" required="required"  placeholder="fullname"/>
    <label for="childname" class="childname"  >Mandatory Field</label>
    <input  ng-model="serverinfo.field" type="text" required="required"  placeholder="city/county.."/>
    <label for="childname" class="childname"  >Field values</label>
    <input  ng-model="serverinfo.value" type="text" required="required"  placeholder=""/>
    <i ng-click="removechild()" ng-show="$last" style="padding-left:16em;" class="material-icons">remove</i>                                    
</div>
<i ng-click="addchild()" style="padding-left:16em;" class="material-icons">add</i>

JS part:

$scope.addchild  = function() {
    //  $scope.child = true;
    var newItemNo = $scope.choices.length+1;
    $scope.choices.push({'id':'choice'+newItemNo});
};

$scope.removechild  = function() {
    var lastItem = $scope.choices.length-1;
    $scope.choices.splice(lastItem);
};

My output is like this,enter image description here

My issue is like whatever I input in the textbox, it will automatically copy to the next set. Can anyone figure out the issue.

9
  • Why do you always bind the values to serverinfo? Shouldn't you bind it to choice in? Commented Apr 25, 2017 at 11:49
  • thank you for your suggestion, i will try to implement that Commented Apr 25, 2017 at 11:50
  • 1
    You should know that after this change you won't have anything inside $scope.serverinfo. If you want to use serverinfo in the controller then you should use serverinfo[ $index ].yourPropertyName instead - This will give you an array where the properties of the elements in the $scope.serverinfo array match the choices in $scope.choices respectively Commented Apr 25, 2017 at 11:53
  • Thank you it worked Commented Apr 25, 2017 at 11:54
  • Alon can you post this as an answer Commented Apr 25, 2017 at 11:57

3 Answers 3

4

You're currently binding all the values to the same object serverinfo, and because you're inside a loop (ng-repeat) then each field in the loop is bound to the same object in the controller.

You have two options:

Bind the fields to the choice element:

<div ng-show="child" class="childDiv" data-ng-repeat="choice in choices track by $index">
    <input  ng-model="choice.childname" type="text" required="required"  placeholder="name"/>
</div>

The above will bind the properties to each choice directly, and will be available in the controller via console.log( $scope.choices[0].childname );

Use the $index indicator to create an array of matched choices:

This is a good solution for cases when you don't want to overwrite/change the values of the original array.

<div ng-show="child" class="childDiv" data-ng-repeat="choice in choices track by $index" ng-init="serverinfo[ $index ].id = choice.id">
    <input  ng-model="serverinfo[ $index ].childname" type="text" required="required"  placeholder="name"/>
</div>

The above will bind the properties to a new array object name serverinfo, where each element is relative to a choice in $scope.choices, it will be available in the controller via console.log( $scope.serverinfo[0].childname );

Note that I also added ng-init="serverinfo[ $index ].id = choice.id" to copy the id property of each choice to the new array elements that are dynamically created by the ngRepeat directive loop.

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

6 Comments

Hey, in your previous comment of that "edit saved" thing, I didn't understand what you meant?
@PraveenKumar On that post that was just deleted?
Yup, that same one.
@PraveenKumar Just a sarcastic comment to the editor that changed i'm to I'm and same capitalization to the comment :)
@PraveenKumar I just remembered this comment so I wanted to let them understand that they should not do unneccacery edits (Eddited the link)
|
1

using your repeated item name "choice" instead of serverInfo should solve the issue

                       <div ng-show="child" class="childDiv" data-ng-repeat="choice in choices track by $index">
                                        <label for="childname" class="childname"  >ServerName </label>
                                        <input  ng-model="choice.childname" type="text" required="required"  placeholder="name"/>
                                        <label for="childname" class="childname"  >Fullname</label>
                                        <input  ng-model="choice.childfullname" type="text" required="required"  placeholder="fullname"/>
                                        <label for="childname" class="childname"  >Mandatory Field</label>
                                        <input  ng-model="choice.field" type="text" required="required"  placeholder="city/county.."/>
                                        <label for="childname" class="childname"  >Field values</label>
                                        <input  ng-model="choice.value" type="text" required="required"  placeholder=""/>
                                        <i ng-click="removechild()" ng-show="$last" style="padding-left:16em;" class="material-icons">remove</i>

                                </div>
                                <i ng-click="addchild()" style="padding-left:16em;" class="material-icons">add</i>

1 Comment

And where do you have this serverInfo, What is serverInfo, its only seen in the HTML
0

That is because you are binding same ng model to each repeating object. put

 ng-model="choice.childname"

Do same for other inputs. And if you want serverinfo to contain all this values than copy it.

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.