我尝试在用户点击提交时清除表单的输入字段。围绕这个主题有很多堆栈溢出问题,但它们都有令人困惑和相互矛盾的答案。
我的表单的HTML:
<form name="form.myform" validate>
<div style="margin-top:25px">
<div style="padding:10px" class="row my-input-divs">
<div class="col-9"><input name='comment' id='comment' ng-model="admin.reply" placeholder=" (Type here to send message to guest) " style="height:50px;background-color:#EFF3F6;width:100%" required style="" type="text"></div>
<div class="col-3"> <input ng-click="sendreply(admin.reply, data.conversation[0].originator,data.conversation[0].property_id)" class="my-btn" required style="width:100%;margin-top:7px" value="Send Reply" type="submit" /></div>
</div>
</div>
</form>
在提交表单的成功函数中,我有:
$('#comment').val('');
$scope.form.myform.$setPristine();
$scope.form.myform.$setUntouched();
我确实使用$scope.form = {};
将表单添加到控制器,而setPristine
和setUntouched
似乎正在运行。
当用户点击提交时,此代码会清除输入字段中的html;但是,它会在被清除时显示错误Please fill out this field
。
此外,代码仍显示绑定到模型。如果用户在从HTML中清除输入后再次单击提交,则会再次提交原始输入。
有谁知道这种行为的原因?