通过更新$ scope模型更正输入后清除角度表单验证

时间:2014-09-23 05:36:43

标签: javascript angularjs

我有一个领域:

<input type="text" class="form-control" name="username" ng-model="user.username" ng-minlength="3" ng-maxlength="20" required ng change="checkUsername()">
<p ng-show="username_taken"  class="help-block">This username is taken.  
<a href="javascript:void(0);" ng-click="suggestUsername()">Shall we suggest one?</a></p>

用户点击Shall后我们建议?,调用suggestUsername()函数并使用唯一的用户名填充用户名($ scope.user.username),用户名更新模型,但表单验证$ valid状态为没有清除。

如何在更新模型后告诉angular重新验证表单?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

将$ setPristine()调用表格

https://docs.angularjs.org/api/ng/type/form.FormController

示例:

你的HTML:

<form name="yourFormName" id="form" novalidate>
   <!-- your form controls here -->
</form>

你的控制器:

// set to pristine, ONLY clears CSS styling, etc...
$scope.yourFormName.$setPristine();

// update your model values as well