javascript Angular 1.2.1 中的 $setPristine() 方法似乎没有按预期工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20010210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$setPristine() method in Angular 1.2.1 doesn't appear to work as intended
提问by dtg
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.
我试图使用$setPristine
AngularJS 中的函数重置文本框,但是它似乎没有导致所需的行为。
My form looks like this:
我的表格是这样的:
<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>
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();
};
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.
虽然该表单显示$pristine
设置为true
在表单条目,然后设置为false
在文本框中输入数据时,提交它确实表明表格后$pristine
设置为true,....,但在文本框中遗体作为值这是在提交按钮被按下之前。
What am I missing here?
我在这里错过了什么?
回答by Davin Tryon
$setPristine
does not clear values from the controls in the form:
$setPristine
不清除表单中控件的值:
From the docs:
从文档:
Sets the form to its pristine state.
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.
将表单设置为其原始状态。
可以调用此方法来删除“ng-dirty”类并将表单设置为其原始状态(ng-pristine 类)。此方法还将传播到此表单中包含的所有控件。
当我们想在保存或重置表单后“重用”表单时,将表单设置回原始状态通常很有用。
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).
从上面的描述中可以看出,$setPristine
只改变了表单的状态(从而重置了应用于表单中每个控件的 css)。
If you want to clear the values of each control, then you need to do for each in code.
如果你想清除每个控件的值,那么你需要在代码中为每个做。
This plunkershows $setPristine
in action.
这个plunker显示$setPristine
在行动中。