$ setPristine()在角1.2.1方法似乎没有发挥预期 [英] $setPristine() method in Angular 1.2.1 doesn't appear to work as intended
问题描述
我已经在尝试重新使用AngularJS的 $ setPristine
功能的文本框,但它似乎并没有产生所需的行为。
I have am trying to reset a textbox using the $setPristine
function in AngularJS, however it doesn't seem to result in the desired behavior.
我的表是这样的:
<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>
和相应的code在我的控制器:
And the corresponding code in my controller:
$scope.sendInvitation = function(userEmail) {
// do some work here ...
// hmm, this doesn't seem to work ...
$scope.addInviteForm.$setPristine();
};
虽然形式显示, $质朴
设置为真正
在表单条目,然后设置为进入假在文本框中数据时,提交表单后,它确实表明
.. ..和但在文本框中的值仍然是它以前的提交按钮是pressed。 $质朴
设置为true
Though the form shows that $pristine
is set to true
upon form entry, then set to false
when entering data in the text-box, after submitting the form it does indeed show that $pristine
is set to true .... and yet the value in the textbox remains as it was before the submit button was pressed.
我是什么在这里失踪?
推荐答案
$ setPristine
不会从表单控件明确值:
$setPristine
does not clear values from the controls in the form:
文档:
设定的形式,它的原始状态。
Sets the form to its pristine state.
此方法可以被调用,以删除'NG-脏'阶级和设置
形成其原始状态(NG-原始类)。这种方法也将
传播到本表格所载的所有控制。
This method can be called to remove the 'ng-dirty' class and set the form to its pristine state (ng-pristine class). This method will also propagate to all the controls contained in this form.
设置形式返回到原始状态,是非常有用
为重复使用保存或复位后一种形式。
Setting a form back to a pristine state is often useful when we want to 'reuse' a form after saving or resetting it.
正如你可以从上面的描述看, $ setPristine
只是改变了形式的状态(由此复位形式应用到每个控件的CSS)。
As you can see from the above description, $setPristine
only changes the state of the form (and thereby resets the css applied to each control in the form).
如果您想清除每个控件的值,那么你需要为每一个code做的。
If you want to clear the values of each control, then you need to do for each in code.
这plunker 显示 $ setPristine
在行动。
这篇关于$ setPristine()在角1.2.1方法似乎没有发挥预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!