angular - “AbstractControl”类型上不存在标识符“控件” - Angular 6
问题描述
我正在尝试使用验证器制作一个注册页面以确认密码。问题是我收到一个错误标识符“控件”未定义。“AbstractControl”不包含这样的成员。我对编程很陌生,不知道为什么会这样
我尝试在谷歌上搜索解决方案,但没有运气
验证器.ts 代码
import { FormControl, FormGroup } from '@angular/forms';
export function passwordMatchValidator(pwSet: FormGroup) {
var password = pwSet.controls.password.value;
var password2 = pwSet.controls.password2.value;
if (!(password === password2)) return {'notmatch': true} ;
return null;
}
component.ts 代码
constructor(private fb: FormBuilder, private authService: AuthService, private router: Router) {}
ngOnInit() {
this.myForm = this.fb.group ({
username: ['', Validators.required],
email: ['', [Validators.required, Validators.email]],
role: ['', Validators.required],
pwSet : this.fb.group ({
password: ['', [Validators.required, Validators.minLength(8)]],
password2: ['', [Validators.required]]
}, {validator: passwordMatchValidator})
});
}
onSubmit() {
this.authService.regUser(this.myForm.value.username, this.myForm.value.email, this.myForm.value.role, this.myForm.get('pwSet.password').value).subscribe(data => {
this.results1 = data;
if (this.results1[0].auth1){
this.exist = true;
}else{
this.router.navigateByUrl('/');
}
});
}
}
component.html 错误:
<mat-form-field formGroupName="pwSet" appearance="fill" class="example-full-width2">
<mat-label>Password</mat-label>
<input formControlName="password" id="password" matInput type="password" >
<mat-error *ngIf="myForm.controls.pwSet.controls.password.dirty &&
myForm.controls.pwSet.controls.password.hasError('minlength')">
Password must have a minimum of 8 characters
</mat-error>
<mat-error *ngIf="myForm.controls.pwSet.controls.password.dirty &&
myForm.controls.pwSet.controls.password.hasError('required')">
Password is <strong>required</strong>
</mat-error>
</mat-form-field>
解决方案
推荐阅读
- javascript - 搜索栏搜索组合 API React
- c - 有没有办法使用指针表示法获取三维向量的大小?
- python - 中间切片的 Pythonic 方式
- java - 响应不成功时如何在改造中读取错误正文?
- for-loop - 如何编写 for 循环以对同一文件夹中包含的多个文件运行回归?
- pandas - pandas 从 apply 函数返回多个 DataFrame
- substrate - 第一个教程:启动你的节点
- javascript - 选择单选按钮时添加弹出下拉列表
- git - Flutter Project size exceeding limit on Git
- sql - Query first slow gets faster after expanding indexes via SMSS