【问题标题】:Issue with ng-model and ng-repeat, duplicate formsng-model 和 ng-repeat 的问题,重复表单
【发布时间】:2014-05-11 06:45:37
【问题描述】:

我有一个基于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 映射对象,而 message 是它里面的一个变量。请让我知道这怎么能被隔离!

【问题讨论】:

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


    【解决方案1】:

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

    查看:

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

    控制器:

    $scope.emails = [];
    

    【讨论】:

      【解决方案2】:

      我也处于angularjs的起步阶段。

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

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

      工作小提琴:Fiddle

      【讨论】:

        猜你喜欢
        • 2014-06-28
        • 1970-01-01
        • 2015-03-08
        • 1970-01-01
        • 2015-08-13
        • 2017-11-19
        • 2012-11-22
        • 2017-12-05
        • 1970-01-01
        相关资源
        最近更新 更多