错误 TS2739:“AbstractControl”类型缺少“FormControl”类型中的以下属性:registerOnChange、registerOnDisabledChange

Jit*_*mar 2 angular angular-reactive-forms

我是 Angular 的新手,正在学习使用Angular 10的在线课程,但有一次卡住了。我正在尝试实现Reactive 表单。为此,我添加了一个新组件text-input。的代码text-input.component.ts如下:

import { Component, Input, Self } from '@angular/core';
import { ControlValueAccessor, NgControl } from '@angular/forms';

@Component({
  selector: 'app-text-input',
  templateUrl: './text-input.component.html',
  styleUrls: ['./text-input.component.css']
})
// We need to implement a control value accessor
export class TextInputComponent implements ControlValueAccessor {
  @Input() label: string;
  @Input() type = 'text';

  constructor(@Self() public ngControl: NgControl) {
    this.ngControl.valueAccessor = this;
  }
  writeValue(obj: any): void {
  }

  registerOnChange(fn: any): void {
  }

  registerOnTouched(fn: any): void {
  }
}
Run Code Online (Sandbox Code Playgroud)

这是代码 text-input.component.html

<div class="form-group">
    <input [class.is-invalid]="ngControl.touched && ngControl.invalid" type="{{type}}" 
    class="form-control"
    [formControl]="ngControl.control" placeholder="{{label}}">

    <div *ngIf='ngControl.control.errors?.required' class="invalid-feedback">Please enter a {{label}}</div>
    <div *ngIf='ngControl.control.errors?.minlength' 
    class="invalid-feedback">{{label}} must be at least {{ngControl.control.errors.minlength['requiredLength']}}</div>

    <div *ngIf='ngControl.control.errors?.maxlength' class="invalid-feedback">{{label}} must be at most {{ngControl.control.errors.maxlength['requiredLength']}}</div>

    <div *ngIf='ngControl.control.errors?.isMatching' class="invalid-feedback">
        Password do not match</div>
</div>
Run Code Online (Sandbox Code Playgroud)

但是一旦我添加了代码,[formControl]我就会开始收到错误消息:

 Type 'AbstractControl' is missing the following properties from type 'FormControl': registerOnChange, registerOnDisabledChange, _applyFormState

4     [formControl]="ngControl.control" placeholder="{{label}}">
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  src/app/_forms/text-input/text-input.component.ts:6:16
    6   templateUrl: './text-input.component.html',
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component TextInputComponent.
Run Code Online (Sandbox Code Playgroud)

课程讲师正在尝试使用此组件在寄存器组件内生成输入控件,代码如下所示 register.component.html

<form [formGroup]="registerForm" (ngSubmit)="register()" autocomplete="off">
    <h2 class="text-center text-primary">Sign up</h2>
    <hr>
    <app-text-input [formControl]='registerForm.controls["username"]' [label]='"Username"'></app-text-input>



    <div class="form-group text-center">
        <button class="btn btn-dark mr-2" type="submit">Register</button>
        <button class="btn btn-success mr-2" (click)="cancel()" type="button">Cancel</button>
    </div>
</form>
Run Code Online (Sandbox Code Playgroud)

并且有一瞥 register.component.ts

import { Component, EventEmitter, OnInit, Output } from '@angular/core';
import { AbstractControl, FormBuilder, FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms';
import { ToastrService } from 'ngx-toastr';
import { AccountService } from '../_services/account.service';

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
  @Output() cancelRegister = new EventEmitter();
  model: any = {};
  registerForm: FormGroup;
  constructor(private accountService: AccountService, private toastr: ToastrService, private fb: FormBuilder) { }

  ngOnInit(): void {
    this.intitializeForm();
  }
  intitializeForm() {
    this.registerForm = this.fb.group({
      username: ['', Validators.required]
      
    })
  } 
  register = () => {
    console.log(this.registerForm.value);
   
  }  
}
Run Code Online (Sandbox Code Playgroud)

请让我知道您还需要什么,我是 Angular 的新手,我无法找到路线原因,但是如果您能帮助我,那么我将继续我的课程,并且非常感谢。

Cod*_*rer 27

这里有各种建议供您AbstractControl选择FormControl。您还可以将模板中的引用替换为例如[formControl]="$any(ngControl.control)"。强制$any()转换运算符是我目前可以为模板引擎找到的唯一内置强制转换运算符。

如果这让您感到困扰,请投票并订阅这个非常受欢迎的长期存在的问题,它实际上可以提供一个不错的解决方案。


Eli*_*seo 14

这是“演员”的问题。您可以创建一个吸气剂(这与我们管理 FormArray 时非常相似,我们创建一个铸造 FormArray 的吸气剂)

get control(){
   return this.ngControl.control as FormControl
}
Run Code Online (Sandbox Code Playgroud)

并将 .html 中的所有ngControl.control内容替换为control


小智 7

您是否在此处以严格模式使用 Angular 11?如果是这样,请更改您的 tsconfig.json 文件并设置:

"strictTemplates": false

并忽略“某些语言功能不可用”的错误。

  • 请注意,这不应该是公认的答案。禁用“strictTemplates”还会禁用与此问题无关的其他功能... (17认同)