我正在使用验证器创建可重用的文本组件
export class CompInputComponent implements OnInit{
@Input() controlFormGroup: FormGroup;
@Input() controlLabel: string;
@Input() controlPlaceHolder: string;
@Input() controlName: string;
@Input() errorMessage: string;
private _isRequired = false;
@Input()
get isRequired(): boolean {
return this._isRequired;
}
set isRequired(val: boolean) {
this._isRequired = coerceBooleanProperty(val);
}
@Output() onComponentReady: EventEmitter<FormGroup> = new EventEmitter<FormGroup>();
constructor(private fb: FormBuilder) { }
ngOnInit(){
console.log(this.isRequired);
if(this.isRequired){
console.log(this.isRequired);
this.controlFormGroup.addControl(this.controlName, new FormControl('', Validators.required));
}else{
this.controlFormGroup.addControl(this.controlName, new FormControl(''));
}
this.onComponentReady.emit(this.controlFormGroup);
}
}
<div [formGroup]="controlFormGroup">
<mat-form-field appearance="outline">
<mat-label>{{controlLabel}}</mat-label>
<input matInput [placeholder]="controlPlaceHolder" [formControlName]="controlName" />
<mat-error *ngIf="controlFormGroup.controls.controlName.hasError('required')">">
<span [innerHTML]="errorMessage"></span>
</mat-error>
</mat-form-field>
</div>
我的问题是我无法设置垫子错误。我有以下错误:
core.js:5967 错误类型错误:无法读取 CompInputComponent_Template 未定义的属性“hasError”
控件的名字不难,而且动态