【发布时间】:2013-11-29 09:14:39
【问题描述】:
我正在尝试使用 AngularJS 中的 $setPristine 函数重置文本框,但它似乎没有产生预期的行为。
我的表单如下所示:
<form name="addInviteForm" ng-controller="InviteCtrl" ng-submit="sendInvitation(userEmail)">
Pristine? {{addInviteForm.$pristine}}
<!-- email input -->
<div>
<input type="email" name="email" ng-model="userEmail" placeholder="Enter email here" class="line-item-input see" required>
<span class="error" ng-show="addInviteForm.email.$error.email" style="color:red">Invalid Email</span>
</div>
<!-- submit button -->
<input type="submit" name="send" class="btn btn-success center" value="Send Invitation">
</form>
以及我的控制器中对应的代码:
$scope.sendInvitation = function(userEmail) {
// do some work here ...
// hmm, this doesn't seem to work ...
$scope.addInviteForm.$setPristine();
};
虽然表单显示$pristine在表单输入时设置为true,然后在文本框中输入数据时设置为false,但在提交表单后确实显示$pristine已设置为 true .... 但文本框中的值仍保持按下提交按钮之前的状态。
我在这里错过了什么?
【问题讨论】:
标签: javascript angularjs dom