简体   繁体   English

Angular反应形式验证

[英]Angular Reactive form validation

I'm working with a Reactive form and I have noticed in some tutorials they do the following: 我正在使用“反应式”表单,并且在一些教程中已经注意到它们执行以下操作:

HTML HTML

.
.
<input type="text" formControlName="firstName" required>
.
.

TS 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? 如果仅在TS文件中可以正常运行,为什么还要在HTML中指定“必需”呢?

Actually, Angular mention something about that here : 实际上,Angular 在这里提到了一些有关此的内容

Caution: Use these HTML5 validation attributes in combination with the built-in validators provided by Angular's reactive forms. 注意:将这些HTML5验证属性与Angular反应式提供的内置验证器结合使用。 Using these in combination prevents errors when the expression is changed after the template has been checked. 组合使用这些选项可防止在检查模板后更改表达式时出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM