angular - Angular4反应式表单验证不起作用
问题描述
在 pic1 我写了一个输入标签(行:101)和另一个从其他地方复制的输入标签(行:102)但是第一个输入标签正在工作,但不是第二个,因为对于第二个输入标签没有“ng-invalid ng -touched” DOM 中的类(图 2)。图 1 图2
import { Component, OnInit } from "@angular/core";
import { FormGroup, FormControl, Validators } from "@angular/forms";
@Component({
selector: "app-sign-up",
templateUrl: "./sign-up.component.html",
styleUrls: ["./sign-up.component.css"]
})
export class SignUpComponent implements OnInit {
ngOnInit(): void {
//this.Registration.va
}
Registration = new FormGroup({
fullName: new FormControl("",[Validators.required])
});
}
解决方案
问题是您对两个不同的输入使用相同的控件。每个表单域都必须有单独的 FormControl。
进行以下更改
signUpComponent.component.ts
Registration = new FormGroup({
fullName1: new FormControl("",[Validators.required]),
fullName2: new FormControl("",[Validators.required])
})
在您的 .html 文件中
<input formControlName="fullName1" id="fullName1" />
<input formControlName="fullName2" id="fullName2" />
推荐阅读
- wso2is - WSO2is SCIM2 过滤器企业属性
- ansible - 在 ansible 中使用 ios_config 时出现错误 ""module_stdout": "", "msg": "MODULE FAILURE\n请参阅 stdout/stderr 了解确切的错误", "rc": 1}"
- sql-server - 连接不同的行数据
- react-native - 如何在本机反应中添加千位分隔符
- node.js - NAPI 插件从 .node 文件中抛出“未定义的符号:omp_get_max_threads”
- jsf - 如何修复在 JBoss EAP 7.3.0 中多次包含 RichFaces 的资源?
- c++ - 通用对象实例化
- mysql - 在 MySQL 中加入多个表
- python - 使用 python 查找维基百科页面标题的完全匹配
- amazon-web-services - 复合弹性搜索仅在两个来源都存在的情况下