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

查看:46
本文介绍了Angular 反应式表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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屋!

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