首页 文章

使用ng-model和ng-repeat问题时,页面上的每个表单字段都会复制输入值

提问于
浏览
0

我有一个页面,其中基于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>

1 回答

  • 0

    我将尝试按照解决方案来实现它 .

    创建一个嵌套的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...
        }
    }
    

相关问题