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

1 回答 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...
    }
}
于 2014-05-10T12:08:21.683 回答