Angular 1.2.1中的$ setPristine()方法似乎没有按预期工作 [英] $setPristine() method in Angular 1.2.1 doesn't appear to work as intended

查看:844
本文介绍了Angular 1.2.1中的$ setPristine()方法似乎没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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>

我的控制器中的相应代码:

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();
    };

虽然表单显示 $ pristine 在表单输入后设置为 true ,然后在提交表单后,在文本框中输入数据时设置为 false 确实显示 $ pristine 设置为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-dirty类,并将
表单设置为其原始状态(ng-pristine类)。此方法也将$ b​​ $ b传播到此表单中包含的所有控件。

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.

当我们想要$ b $时,将表单设置回原始状态通常很有用b在保存或重置之后重新使用表单。

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).

如果你想要清除每个控件的值,那么您需要为每个控件的值执行。

If you want to clear the values of each control, then you need to do for each in code.

这个plunker 显示 $ setPristine

这篇关于Angular 1.2.1中的$ setPristine()方法似乎没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆