ng-model和ng-repeat,重复表格的问题

时间:2014-05-11 06:45:37

标签: angularjs angularjs-directive angularjs-ng-repeat angular-ngmodel

我有一个页面,其中基于ng-repeat创建了多个表单。一切正常,直到在输入中写入内容,并且所有其他重复的表单输入元素上的所有内容都会重复。我使用了ng-model="Notify.message",它只是一个对象,它从输入中获取值并发送到按钮提交控制,从而保留逻辑的其余部分。

我正在寻找何时填写一个表单,其他表单应该保持完整,不应该复制表单1的输入文本中写入的值。

以下是代码:

<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>

问题小提琴 - jsfiddle

在这里,你可以在一个输入中写入某些内容,其他内容也被填充:(。Notify也是一个Java映射对象,消息是一个变量。请让我知道这怎么可以被分解!

2 个答案:

答案 0 :(得分:1)

您将所有输入绑定到$scope上的同一变量。 您必须将每个文本框绑定到$scope上的不同变量:

查看:

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

控制器:

$scope.emails = [];

答案 1 :(得分:0)

我也处于angularjs的起始阶段。

前几天我遇到了同样的问题并通过在ng-model中提供动态模型名称解决了这个问题

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

工作小提琴: Fiddle

相关问题