任何人都可以帮助使用包含的Plunker ??
只需提交表单,只输入一个必填字段。这将触发验证错误。然后重置调用setPristine和setUntouched的表单 - 两者都应该重置表单,但是没有清除验证错误?
我希望表单被清除数据,但也不会显示验证消息(即使我欣赏,因为数据是空白的,实际上是无效的)
即。这个重置功能
$scope.reset = function(){
$scope.newQuestion = angular.copy(tempQuestion);
$scope.forms.setPristine();
$scope.forms.setUntouched();
};
答案 0 :(得分:6)
您正在调用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
答案 1 :(得分:3)
检查您的控制台,您会看到一些错误。对象setPristine()
上不存在forms
,因为您的表单名称为forms.newQuestion
。此外,该功能是$setPristine()
。要在重置时消除错误消息的形式,您只需将attempted
标志重置为false,因为这会导致ng-class
表达式评估为假。
您需要以下内容:
$scope.reset = function(){
$scope.newQuestion = angular.copy(tempQuestion);
$scope.forms.newQuestion.$setPristine();
$scope.forms.newQuestion.$setUntouched();
$scope.attempted = false;
};