我是Angular的新手。我正在动态渲染一些字段到我的响应式表单中。当我使用一个模拟请求(即渲染正确,控制台没有错误)时,一切都很好地工作。但是,一旦我使用ng build
并使用真正的后端,每个我动态渲染的字段都会出现错误:
main.js:1 ERROR TypeError: Cannot read property '_rawValidators' of null
我找不到这个错误的任何背景信息。我很想听听您的想法。
更多背景
// these fields change with selection
this.datafields = [{
dfId: 48,
dfName: "Phone",
dfType: "text",
dfOptions: null,
dfValue: ""
},
{
dfId: 49,
dfName: "Eval",
dfType: "select",
dfOptions: ["","Remote","Live"],
df_value: "",
}]
尝试在ngOnInit
中使用TypeScript呈现(尝试了ngAfterViewInit
但没有改善)
dfGroup = new FormGroup({})
...
...
this.eyeForm = this.formBuilder.group({
focus: ['', Validators.required],
datafields: this.formBuilder.array([])
})
...
...
if (this.datafields != null || this.datafields != undefined) {
this.datafields.forEach((x:any) => {
this.dfGroup.setControl(x.dfName, new FormControl(x.dfValue));
});
this.getDataFields.push(this.dfGroup);
}
HTML看起来像下面这样:
<div [formGroup]="dfGroup">
<div class="row pt-2" *ngFor="let field of datafields; let i=index">
<div class="col-4 d-flex align-items-center 13required">
{{field.dfName}}
</div>
<div class="col-6">
<mat-form-field *ngIf="field.dfType == 'text'" appearance="outline">
<input
matInput
[type]="field.dfType"
[formControlName]="field.dfName"
required
/>
</mat-form-field>
<mat-form-field
*ngIf="field.dfType == 'select'"
appearance="outline"
>
<mat-select [formControlName]="field.dfName" placeholder="">
<mat-option
[value]="option"
*ngFor="let option of field.dfOptions"
>
{{ option }}
</mat-option>
</mat-select>
</mat-form-field>
</div>
</div>
</div>
<app-foo [formControl]="formControl"/>
),但失败了,因为formControl
似乎也被用作指令。使用输入别名解决了我的问题(即:<app-foo [fwdFormControl]="formControl"/>
)。 - fservantdev