错误TS2739:“AbstractControl”类型缺少“FormControl”类型中的以下属性:registerOnChange、registerOnDisabledChange
我是 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 {
}
}
这是代码 text-input.component.html
<div>
<input [class.is-invalid]="ngControl.touched && ngControl.invalid" type="{{type}}"
[formControl]="ngControl.control" placeholder="{{label}}">
<div *ngIf='ngControl.control.errors?.required'>Please enter a {{label}}</div>
<div *ngIf='ngControl.control.errors?.minlength'
>{{label}} must be at least {{ngControl.control.errors.minlength['requiredLength']}}</div>
<div *ngIf='ngControl.control.errors?.maxlength'>{{label}} must be at most {{ngControl.control.errors.maxlength['requiredLength']}}</div>
<div *ngIf='ngControl.control.errors?.isMatching'>
Password do not match</div>
</div>
但是一旦我添加了代码,[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.
课程讲师正在尝试使用此组件在寄存器组件内生成输入控件,代码如下所示 register.component.html
<form [formGroup]="registerForm" (ngSubmit)="register()" autocomplete="off">
<h2>Sign up</h2>
<hr>
<app-text-input [formControl]='registerForm.controls["username"]' [label]='"Username"'></app-text-input>
<div>
<button type="submit">Register</button>
<button (click)="cancel()" type="button">Cancel</button>
</div>
</form>
并且有一瞥 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);
}
}
请让我知道您还需要什么,我是 Angular 的新手,我无法找到路线原因,但是如果您能帮助我,那么我将继续我的课程,并且非常感谢。
回答
您是否在此处以严格模式使用 Angular 11?如果是这样,请更改您的 tsconfig.json 文件并设置:
"strictTemplates": false
并忽略“某些语言功能不可用”的错误。
以上是错误TS2739:“AbstractControl”类型缺少“FormControl”类型中的以下属性:registerOnChange、registerOnDisabledChange的全部内容。
THE END
二维码