标签: angular-controlvalueaccessor

Angular 12 - 类型错误:无法读取 null 的属性(读取“writeValue”)

Angular 12 - 类型错误:无法读取 null 的属性(读取“writeValue”)

我正在创建一个通用文本输入组件,在为项目提供服务时一切正常,但在构建的项目中我收到此错误:

类型错误:无法读取 null 的属性(读取“writeValue”)

HTML:

<div class="p-field">
    <label>{{label}} <span class="p-error">{{checkRequired(ngControl.control) ? '*' : ''}}</span></label>
    <input class="full-width" [type]="type" pInputText [formControl]="ngControl.control">

    <small *ngIf="ngControl.control.touched && ngControl.control.errors?.required" class="p-error">{{label}} is required</small>


    <small *ngIf="ngControl.control.errors?.minlength" class="p-error">
        {{label}} must be at least {{ngControl.control.errors.minlength['requiredLength']}}
    </small>

    <small *ngIf="ngControl.control.errors?.maxlength" class="p-error">
        {{label}} must be at most {{ngControl.control.errors.maxlength['requiredLength']}}
    </small>
    <small *ngIf="ngControl.control.errors?.email" class="p-error">
        This email is not valid
    </small>

    <small *ngIf="ngControl.control.errors?.isMatching" class="p-error">Passwords do not match</small>
</div>
Run Code Online (Sandbox Code Playgroud)

组件.ts

import { Component, Input, OnInit, Self } from '@angular/core';
import {AbstractControl, …
Run Code Online (Sandbox Code Playgroud)

angular angular-reactive-forms angular-controlvalueaccessor

9
推荐指数
3
解决办法
2万
查看次数

Angular V11:NullInjectorError:没有 ControlContainer 的提供者

我有一个根据本文制作的自定义输入:medium.com:dont-reinvent-the-wheel

\n

这是我的代码,它处于严格模式 \xe2\x96\xbc

\n
// input.component.ts\n\nimport { Component, Input, ViewChild } from \'@angular/core\';\nimport {\n    ControlContainer,\n    ControlValueAccessor,\n    FormControl,\n    FormControlDirective,\n    NG_VALUE_ACCESSOR\n} from \'@angular/forms\';\nimport {\n    FloatLabelType,\n    MatFormFieldAppearance\n} from \'@angular/material/form-field\';\n\n@Component({\n    selector: \'app-input\',\n    templateUrl: \'./input.component.html\',\n    styleUrls: [\'./input.component.scss\'],\n    providers: [\n        {\n            provide: NG_VALUE_ACCESSOR,\n            useExisting: InputComponent,\n            multi: true\n        }\n    ]\n})\nexport class InputComponent implements ControlValueAccessor {\n    isDisabled!: boolean;\n\n    @Input() isRequired!: boolean;\n\n    @Input() label!: string;\n\n    @Input() placeholder!: string;\n\n    @Input() readonly!: boolean;\n\n    @Input() appearance: MatFormFieldAppearance = \'fill\';\n\n    @Input() floatLabel: FloatLabelType = \'auto\';\n\n    @ViewChild(FormControlDirective, { static: true })\n …
Run Code Online (Sandbox Code Playgroud)

angular angular-reactive-forms controlvalueaccessor angular-controlvalueaccessor

1
推荐指数
1
解决办法
2484
查看次数