Angular 1.2.1中的$ setPristine()方法似乎没有按预期工作

dtg*_*dtg 2 javascript dom angularjs

我正在尝试使用$setPristineAngularJS中的函数重置文本框,但它似乎不会导致所需的行为.

我的表单看起来像这样:

<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>
Run Code Online (Sandbox Code Playgroud)

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

$scope.sendInvitation = function(userEmail) {

        // do some work here ...

        // hmm, this doesn't seem to work ...
        $scope.addInviteForm.$setPristine();
    };
Run Code Online (Sandbox Code Playgroud)

虽然表单显示$pristine设置为true表单条目,然后设置为false在文本框中输入数据时,在提交表单后它确实显示$pristine设置为true ....然而文本框中的值仍然为它是在按下提交按钮之前.

我在这里错过了什么?

Dav*_*yon 7

$setPristine 不清除表单中控件的值:

来自文档:

将表单设置为其原始状态.

可以调用此方法来删除'ng-dirty'类并将表单设置为其原始状态(ng-pristine类).此方法还将传播到此表单中包含的所有控件.

当我们想要在保存或重置表单后"重用"表单时,将表单设置回原始状态通常很有用.

从上面的描述中可以看出,$setPristine只改变了表单的状态(从而重置了应用于表单中每个控件的css).

如果要清除每个控件的值,则需要为每个代码执行操作.

这个plunker显示$setPristine在行动.

  • 虽然我会说,文档根本没有说清楚.即,控件的"状态"与控件的当前"值"之间的语义区别并不明显...... (5认同)