Angular 反应式表单验证 [英] Angular Reactive form validation
问题描述
我正在使用 Reactive 表单,我注意到在一些教程中他们执行以下操作:
HTML
<预><代码>..<input type="text" formControlName="firstName" required>..TS
<预><代码>..名字:['',Validators.required]..问题:
为什么我需要在 HTML 中指定 "required" 如果我只是在 TS 中指定> 文件工作正常吗?
实际上,Angular 提到了一些关于这里:
<块引用>注意:将这些 HTML5 验证属性与Angular 的响应式表单提供的内置验证器.使用这些组合可防止在表达式更改后出现错误模板已被检查.
I'm working with a Reactive form and I have noticed in some tutorials they do the following:
HTML
.
.
<input type="text" formControlName="firstName" required>
.
.
TS
.
.
firstName: ['', Validators.required]
.
.
QUESTION:
Why do I need to specify "required" in the HTML if I just do it in the TS file it works fine?
Actually, Angular mention something about that here:
Caution: Use these HTML5 validation attributes in combination with the built-in validators provided by Angular's reactive forms. Using these in combination prevents errors when the expression is changed after the template has been checked.
这篇关于Angular 反应式表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!