【发布时间】: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