javascript - AngularJs setPristine() 不重置无效标志

标签 javascript angularjs

任何人都可以帮助使用随附的 Plunker 吗??

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

我希望清除表单中的数据,但也不显示任何验证消息(尽管我很欣赏数据是空白的,但实际上它是无效的)

即这个重置函数

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

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

Plunker example

最佳答案

  1. 您错误地调用了 setPristine() 和 setUntouched()。

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

    参见 this page了解更多详情

  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;

  };

这是修改后的 Plunker

关于javascript - AngularJs setPristine() 不重置无效标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31969456/

相关文章:

javascript - 如何在jqgrid中加载新添加的数据

javascript - 循环和数组问题

javascript - Cytoscape.js 警告 : "The style value of ` label` is deprecated for `width` "when trying to have a node with same width of label

javascript - jquery xml解析问题

AngularJS ui 路由器强制参数

javascript - Angularjs - 为什么我在使用 Google 自动完成地址表单时收到此 InvalidValue 错误

javascript - 如何创建回文检查器?

php - 哈希 url(片段)可以被搜索引擎抓取吗?

javascript - 使用动态配置选项动态加载 highcharts

javascript - 如何使用 Angular.js 在单个类中添加多个条件