【问题标题】:Issue with ng-model and ng-repeat, input value is duplicated on each form field on the pageng-model 和 ng-repeat 的问题,输入值在页面上的每个表单字段上重复
【发布时间】:2014-06-28 03:22:13
【问题描述】:

我有一个基于 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>

【问题讨论】:

  • 由于您将所有输入ng-model 绑定到Notify.message,如果有人更新它,其他表单会因为数据绑定而得到它吗?您应该为每个重复输入设置单独的模型,以使其独立工作
  • @Chandermani - 我怎样才能有单独的模型?有没有办法为每个新表单重新初始化它?
  • 这取决于场景。如果您使用ng-model=message。每个重复范围都将包含其自己的消息属性,您可以在重复范围内访问该属性。否则,您需要以一种可以将不同模型绑定到不同形式的方式(如 @aamir 建议的方式)构建模型。
  • @Chandermani - 对不起,我对它完全陌生,所以你可以通过更新这个小提琴来帮助这个 - jsfiddle.net/CMnxW/1
  • 你想要这样的东西jsfiddle.net/4K8e7

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


【解决方案1】:

我将尝试以下解决方案来实现它。

创建一个嵌套的 json 对象,其中包含要在 angularjs 控制器中显示的表单数。

例如

$scope.FormsCollection = {
         "Form1" : [
             {"title" : "Rockband", "details" : "1hr"},
         ],
         "Form2" : [
             {"title" : "The Old You", "details" : "Dr. Smith"}
         ]

}

显然,您可以使用循环来构建它,或者在您的上下文中最适合您的任何方法来构建 angularjs 控制器中的表单集合。

然后在 html 页面中,您可以使用以下约定来正确填充每个表单数据

你需要两个 ng-repeat,首先是每个表单的索引,然后是迭代嵌套的表单对象。

<input type="text" ng-model="form[index].firstName"/>

因此,您将拥有具有正确表单对象数据的 $scope.FormsCollection

请查看以下jsfiddle中的示例代码

http://jsfiddle.net/CMnxW/4/

jsfiddle 应该包含以下代码。

<div ng-app>
    <div ng-controller="controller">
        <ul ng-repeat="post in posts">
            <li>{{$index}}
                <input type="text" ng-model="post.userEmail"  />
                <button class="btn btn-danger" ng-click="notify(post)" type="button">Notify</button>
            </li>
        </ul>

        <table>
            <tr>
                <td>destEmail is: </td>
                <td>{{Notify.destEmail}}</td>
            </tr>
        </table>
    </div>
</div>

JS:

function controller($scope) {
    $scope.Notify = {
        destEmail: ''
    };

    $scope.posts = [{userEmail: 'e@mail.com'}, {userEmail: 'f@mail.com'}];

    $scope.notify = function(post) {
        $scope.Notify.destEmail = post.userEmail;
        //set other $scope.Notify properties from post
        //your other notify code...
    }
}

【讨论】:

  • 你能帮我一把小提琴吗?
  • @AngryJS 您希望显示与按钮单击相关的相应电子邮件。就像如果第一个通知按钮单击您要打印(e@mail.com),然后在第二个按钮上单击打印(f@email.com)。如果我误解了,请确认或纠正我。
  • 实际上我的要求是将表单提交上的电子邮件字段发送到控制器,控制器将映射到 Notify.message 变量 {java class VO}。所以就像每次点击按钮时,在电子邮件中输入的值都会被发送并输入到数据库中
  • @AngryJS 我更新了我的答案,请验证更新 jsfiddle 示例。
  • @AngryJS 这是一种编程风格。我什至根本不会使用 Notify.destEmail。我将简单地在通知功能中使用 post.Email。或者我可以在用户输入所需的电子邮件后使用 angularjs 循环来发布每封电子邮件,我将遍历帖子,或者我可以将整个(帖子)集合发送到服务器,并循环处理请求。所以考虑到问题的背景,这是一个选择问题
猜你喜欢
  • 1970-01-01
  • 2012-11-22
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 2015-08-13
  • 2015-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多