任何人都可以帮助使用随附的 Plunker 吗??
只需提交仅输入一个必填字段的表单。这将触发验证错误。然后重置同时调用 setPristine 和 setUntouched 的表单 - 两者都应该重置表单但是都没有清除验证错误?
我希望清除表单中的数据,但也不显示任何验证消息(尽管我很欣赏数据是空白的,但实际上它是无效的)
即这个重置函数
$scope.reset = function(){
$scope.newQuestion = angular.copy(tempQuestion);
$scope.forms.setPristine();
$scope.forms.setUntouched();
};
最佳答案
您错误地调用了 setPristine() 和 setUntouched()。
正确的语法是
formName.$setPristine()
和formName.$setUntouched()
参见 this page了解更多详情
由于您在 中设置了
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/