AngryJS
AngryJS

Reputation: 955

Issue with ng-model and ng-repeat, duplicate forms

I have a page where multiple forms are created based on ng-repeat. Everything works fine until write something into the input and everything gets duplicated on all the other repeated forms input elements. I have used ng-model="Notify.message" which is nothing but object which takes the value from the input and sends to control on button submit and hence rest of the logic.

I am looking for when if one form is been filled, other forms should keep quite and shouldn't duplicate the values written in input text of form 1.

Here is the code:

<div data-ng-show="alluserposts.length > 0">
    <div id="b{{userpost.id}}" data-ng-repeat="userpost in alluserposts" >
            <div class="row" style="margin-left: -5px">
                <form class="text-center" role="form" id=f1{{userpost.id}} name="userForm"
                      ng-submit="notify(userForm.$valid, userpost, apiMe)" novalidate>
                    <div class="row">
                        <div class="col-xs-8 col-md-4">
                            <div class="form-group">
                                <input data-container="body" data-toggle="popover" data-placement="top"
                                       data-content="Any message which you would like to convey to post owner"
                                       type="text" ng-model="Notify.message" data-ng-init="Notify.message=''"
                                       id="u{{userpost.id}}"
                                       placeholder="Enter a Message or Phone number" class="form-control"
                                       required>

                                <p ng-show="userForm.name.$invalid && !userForm.name.$pristine" class="help-block">It is
                                    required.</p>
                                <script>$(function () {
                                    $("[data-toggle='popover']").popover();
                                });
                                </script>

                                <input type="hidden" ng-model="Notify.loggedInEmail"
                                       ng-init="Notify.loggedInEmail = result.email"/>
                                <input type="hidden" ng-model="Notify.postId" ng-init="Notify.postId = userpost.id"/>
                                <input type="hidden" ng-model="Notify.destEmail"
                                       ng-init="Notify.destEmail = userpost.userEmail"/>
                            </div>
                        </div>

                        <div ng-show="loginStatus.status == 'connected'" class="col-xs-4 col-md-2">
                            <button class="btn btn-primary" ng-disabled="userForm.$invalid || !userForm.$dirty"
                                    type="submit">
                                Notify Post Owner
                            </button>
                        </div>
                    </div>
                </form>
                </p>
            </div>
        </div>
    </div>
</div>

Issue fiddle - jsfiddle

Here you can when something is written in one input, other gets filled too :( . Also Notify is a Java mapped object and message is a variable inside it. Pls let me know how can this can be segragated!

Upvotes: 1

Views: 2252

Answers (2)

ABC
ABC

Reputation: 4373

I am also at the starting phase of angularjs.

I have faced the same issue few days ago and resolved it by providing dynamic model name in ng-model like

<input type="text" ng-model="Notify[post.userEmail]" ng-init="Notify[post.userEmail] = post.userEmail" />

Working fiddle: Fiddle

Upvotes: 0

MRB
MRB

Reputation: 3812

You bind all of your inputs to same variable on $scope. You must bind every text box to a distinct variable on $scope:

View:

<ul ng-repeat="post in posts">
    <li>{{$index}}
        <input type="text" ng-model="emails[$index]"/>
    </li>
</ul>

Controller:

$scope.emails = [];

Upvotes: 1

Related Questions