Angular Forms 的问题:AbstractControl 与 FormControl

Nin*_*420 3 angular angular-forms

当我尝试使用带有控件的表单时,出现此错误。

 Type 'AbstractControl' is missing the following properties from type 
'FormControl': registerOnChange, registerOnDisabledChange, _applyFormState
Run Code Online (Sandbox Code Playgroud)

表格代码

  this.checkoutForm = this.fb.group({
      firstName: ['', [Validators.required, Validators.pattern('[a-zA-Z][a-zA-Z ]+[a-zA-Z]$')]],
      lastName: ['', [Validators.required, Validators.pattern('[a-zA-Z][a-zA-Z ]+[a-zA-Z]$')]],
      phoneNumber: ['', [Validators.required, Validators.pattern('[0-9]+')]],
      address: ['', [Validators.required, Validators.maxLength(100)]],
      pinCode: ['', Validators.required]
    });
Run Code Online (Sandbox Code Playgroud)

html

<input type="text" 
name="firstName"
[formControl]="checkoutForm.controls['firstName']" 
value="" 
placeholder="" 
autocomplete="off"      
>
Run Code Online (Sandbox Code Playgroud)

小智 7

当您创建响应式表单时,您应该使用表单本身,而不是它的控件。

如果您只是单独使用控件,那么拥有表单的意义何在?

<form [formGroup]="checkoutForm">
  <input type="text" formControlName="firstName">
</form>
Run Code Online (Sandbox Code Playgroud)