简体   繁体   English

Angular V11:NullInjectorError:没有 ControlContainer 的提供程序

[英]Angular V11: NullInjectorError: No provider for ControlContainer

I have a custom input made in accordance to this article: medium.com: dont-reinvent-the-wheel .我根据本文进行了自定义输入: medium.com: dont-reinvent-the-wheel

Here is my code, it is in strict mode ▼这是我的代码,它处于严格模式 ▼

// input.component.ts

import { Component, Input, ViewChild } from '@angular/core';
import {
    ControlContainer,
    ControlValueAccessor,
    FormControl,
    FormControlDirective,
    NG_VALUE_ACCESSOR
} from '@angular/forms';
import {
    FloatLabelType,
    MatFormFieldAppearance
} from '@angular/material/form-field';

@Component({
    selector: 'app-input',
    templateUrl: './input.component.html',
    styleUrls: ['./input.component.scss'],
    providers: [
        {
            provide: NG_VALUE_ACCESSOR,
            useExisting: InputComponent,
            multi: true
        }
    ]
})
export class InputComponent implements ControlValueAccessor {
    isDisabled!: boolean;

    @Input() isRequired!: boolean;

    @Input() label!: string;

    @Input() placeholder!: string;

    @Input() readonly!: boolean;

    @Input() appearance: MatFormFieldAppearance = 'fill';

    @Input() floatLabel: FloatLabelType = 'auto';

    @ViewChild(FormControlDirective, { static: true })
    formControlDirective!: FormControlDirective;

    @Input() formControl!: FormControl;

    @Input() formControlName!: string;

    get control(): FormControl {
        return (
            this.formControl ||
            this.controlContainer.control?.get(this.formControlName)
        );
    }

    constructor(private controlContainer: ControlContainer) {}

    clearInput(): void {
        this.control.setValue('');
    }

    registerOnTouched(fn: any): void {
        this.formControlDirective.valueAccessor?.registerOnTouched(fn);
    }

    registerOnChange(fn: any): void {
        this.formControlDirective.valueAccessor?.registerOnChange(fn);
    }

    writeValue(obj: any): void {
        this.formControlDirective.valueAccessor?.writeValue(obj);
    }

    setDisabledState(disabled: boolean): void {
        this.isDisabled = disabled;
    }
}
<!-- input.component.html -->

<div class="custom-input">
    <mat-form-field
        [appearance]="appearance"
        [floatLabel]="floatLabel"
        class="custom-input__form-field"
    >
        <mat-label class="custom-input__label"
            >{{ label }} <span *ngIf="isRequired && label">*</span></mat-label
        >
        <input
            class="custom-input__value"
            matInput
            type="text"
            [formControl]="$any(control)"
            [placeholder]="placeholder"
            [readonly]="readonly"
        />
        <button
            class="custom-input__clear-btn"
            matSuffix
            mat-icon-button
            aria-label="Clear"
            *ngIf="$any(control).value && !readonly"
            (click)="clearInput()"
        >
            <mat-icon>close</mat-icon>
        </button>
    </mat-form-field>
</div>

There is no compiler error but the browser log this error ▼没有编译错误但是浏览器会记录这个错误▼

ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(ClientsListModule)[ControlContainer -> 
ControlContainer -> ControlContainer -> ControlContainer -> ControlContainer -> ControlContainer]: 
  NullInjectorError: No provider for ControlContainer!
NullInjectorError: R3InjectorError(ClientsListModule)[ControlContainer -> ControlContainer -> ControlContainer -> 
ControlContainer -> ControlContainer -> ControlContainer]: 
  NullInjectorError: No provider for ControlContainer!

I've checked similar questions here Angular 5: "No provider for ControlContainer" and here No provider for ControlContainer and according to them issue is in not importing both ReactiveFormsModule and FormsModule in respective module, but I am importing them and still I see that error.我在这里检查了类似的问题Angular 5: "No provider for ControlContainer"和这里No provider for ControlContainer并且根据他们的问题是没有在各自的模块中同时导入ReactiveFormsModuleFormsModule ,但我正在导入它们,但我仍然看到该错误.

在此处输入图像描述

UPDATE 1: In components <app-input> is used in the following way ▼更新 1:在组件中<app-input>以下列方式使用 ▼

在此处输入图像描述

As it seems you use formControl without formGroup in some cases, so use @Optional decorator to have controlContainer null in those cases.在某些情况下,您似乎使用了没有formControlformGroup ,所以在这些情况下使用@Optional装饰器来拥有controlContainer null。

constructor(@Optional() private controlContainer: ControlContainer) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM