Skip to content

how should fix the error: Type 'AbstractControl' is missing the following properties from type 'FormControl' #11

@new2690

Description

@new2690

i use angular 12.1.1. hi in section 12 (reactive forms) after creating the text-input component we and declare all inputs in the ts file and implement ControlValueAccessor interface I get error in text-input HTML file when I trying to type [formControl]="ngControl.control". error message is that:
Type 'AbstractControl' is missing the following properties from type 'FormControl': registerOnChange, registerOnDisabledChange, _applyFormState

text-input.component.html code:

<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">
        Passwords do not match
    </div>
</div>

text-input.component.ts code:

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']
})
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 {    
  }
  setDisabledState?(isDisabled: boolean): void {    
  }
}

please help me how i should fix this thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions