首页 > 解决方案 > “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 错误:

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>

标签: angulartypescriptangular-forms

解决方案


推荐阅读