AngularJs setPristine()不重置无效标志

Mad*_*die 3 javascript angularjs

任何人都可以帮助包括Plunker?

只需提交表单,只输入一个必填字段.这将触发验证错误.然后重置调用setPristine和setUntouched的表单 - 两者都应该重置表单但是没有清除验证错误?

我希望表单被清除数据,但也没有显示验证消息(即使我欣赏,因为数据是空白的,它实际上是无效的)

即这个重置功能

$scope.reset = function(){
    $scope.newQuestion = angular.copy(tempQuestion);

    $scope.forms.setPristine();
    $scope.forms.setUntouched();
};
Run Code Online (Sandbox Code Playgroud)

Plunker的例子

小智 6

  1. 您正在错误地调用setPristine()和setUntouched().

    正确的语法是formName.$setPristine()formName.$setUntouched()

    有关详细信息,请参阅此页面

  2. 由于您正在设置attempted=true,当您要重置表单时,您还必须重置该值attempted.这将确保ng-if重新评估并不呈现错误消息.

所以reset()的正确代码如下:

$scope.reset = function(){
    $scope.newQuestion = angular.copy(tempQuestion);

    $scope.forms.newQuestion.$setPristine();
    $scope.forms.newQuestion.$setUntouched();

    $scope.attempted = false;

  };
Run Code Online (Sandbox Code Playgroud)

这是修改过的Plunker