我想在提交数据之前检查表单字段的所有验证,点击提交按钮并激活缺失或错误的字段验证
就像之前版本的angular on submit一样,如果表单状态无效,我们可以触发所有错误验证
if (scope.sampleForm.$valid) {
// success code
} else {
angular.forEach(scope.sampleForm.$error, function (field) {
angular.forEach(field, function (errorField) {
errorField.$setTouched();
})
});
}
我检查了很多与此相关的答案,如果表格无效,大多数答案都是禁用提交按钮,我想在没有禁用按钮的情况下实现这一点,请建议我如何实现这一目标?我正在使用模板驱动的方法角(v4)
2 回答
如何引入一个新变量,例如
submitted
,然后为表单控件firstname
设置类似下面的条件:在您的提交方法上,如果表单无效,您只需将此标志设置为
true
:TS:
您必须将html name属性添加到表单的每个输入 . name属性指定表单实例上的属性名称 .
您可以使用表单控件并检查字段是否脏 . 检查这两个文件以便更好地理解
https://github.com/rahulrsingh09/AngularConcepts/blob/master/src/app/template-driven/template-driven.component.html
https://github.com/rahulrsingh09/AngularConcepts/blob/master/src/app/template-driven/template-driven.component.ts
https://rahulrsingh09.github.io/AngularConcepts/#/template