Angular反应形式验证 [英] Angular Reactive form validation

查看:75
本文介绍了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]
.
.

问题:

如果我只是在 TS 中为什么要在 HTML 中指定 必需" >可以正常工作吗?

Why do I need to specify "required" in the HTML if I just do it in the TS file it works fine?

推荐答案

实际上,Angular提到了有关

Actually, Angular mention something about that here:

警告:将这些HTML5验证属性与Angular的反应式提供的内置验证器.使用这些结合使用可防止在表达式更改后出现错误模板已检查.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆