英文:
RegisterOnChange is not a function
问题
我使用FormBuilder(fb)设计了这个表单。
this.serviceRecordForm = this.fb.group({
client: [],
serviceRecordItem: this.fb.group({
productStatus: [''],
password: [''],
hasBackup: [''],
deliveryType: [''],
product: [''],
brand: [''],
serialNumber: [''],
defect: [''],
}),
accesoryList: this.fb.array([''])
});
accesoryList是从后端获取的数组。我的HTML表单如下:
<form [formGroup]="serviceRecordForm" (ngSubmit)="save()">
<div class="mb-3" *ngFor="let accessory of accesoryList">
<label for="{{accessory.id}}" class="form-label">{{accessory.name}}
<input type="checkbox" id="{{accessory.id}}" formControlName="accesoryList"
[ngModel]="accessory">
</label>
</div>
</form>
我在ngOnInit()方法中从Restful API中填充accesoryList。
this.accesoryService.getAccesories().subscribe(accessories => {
this.accesoryList = accessories;
})
每当我打开表单时,我可以看到它从后端正确设计并填充了accesoryList,但我在控制台中会得到与accesoryList.length相同次数的错误。
ERROR TypeError: control.registerOnChange is not a function
at setUpModelChangePipeline (forms.mjs:3117:13)
at setUpControl (forms.mjs:2947:5)
at FormGroupDirective.addControl (forms.mjs:4771:9)
at FormControlName._setUpControl (forms.mjs:5321:43)
at FormControlName.ngOnChanges (forms.mjs:5266:18)
at FormControlName.rememberChangeHistoryAndInvokeOnChangesHook (core.mjs:1521:14)
at callHook (core.mjs:2444:18)
at callHooks (core.mjs:2403:17)
at executeInitAndCheckHooks (core.mjs:2354:9)
at refreshView (core.mjs:10341:21)
我使用的是Angular版本15和带有Spring Data JPA的Spring Boot后端。
英文:
I designed this form using FormBuilder(fb)
this.serviceRecordForm = this.fb.group({
client: [],
serviceRecordItem: this.fb.group({
productStatus: [''],
password: [''],
hasBackup: [''],
deliveryType: [''],
product: [''],
brand: [''],
serialNumber: [''],
defect: [''],
}),
accesoryList: this.fb.array([''])
});
accesoryList is an array coming from backend. And my HTML form is :
<form [formGroup]="serviceRecordForm" (ngSubmit)="save()">
<div class="mb-3" *ngFor="let accesory of accesoryList">
<label for="{{accesory.id}}" class="form-label">{{accesory.name}}
<input type="checkbox" id="{{accesory.id}}" formControlName="accesoryList"
[ngModel]="accesory"></label>
</div>
</form>
I am populating this accesoryList inside ngOnInit() method from Restful API.
this.accesoryService.getAccesories().subscribe(accesories => {
this.accesoryList = accesories;
})
Whenever i open my form i can see it properly designed and populated accesoryList from backend but i got this error as much as number of accesoryList.length inside console.
ERROR TypeError: control.registerOnChange is not a function
at setUpModelChangePipeline (forms.mjs:3117:13)
at setUpControl (forms.mjs:2947:5)
at FormGroupDirective.addControl (forms.mjs:4771:9)
at FormControlName._setUpControl (forms.mjs:5321:43)
at FormControlName.ngOnChanges (forms.mjs:5266:18)
at FormControlName.rememberChangeHistoryAndInvokeOnChangesHook (core.mjs:1521:14)
at callHook (core.mjs:2444:18)
at callHooks (core.mjs:2403:17)
at executeInitAndCheckHooks (core.mjs:2354:9)
at refreshView (core.mjs:10341:21)
i use angular v15 and spring boot backend with spring data jpa
答案1
得分: 1
你正在错误地使用formArray
。在模板中,你应该使用formArrayName
和index
,并且在从后端获取列表时需要添加formControl
。
所以在 TypeScript 代码中应该像这样:
const accesoryListControl = this.form.get('accesoryList') as FormArray;
this.accesoryList.forEach((accesory) => {
accesoryListControl.push(new FormControl(accesory.checked));
});
在 HTML 中应该像这样:
<div formArrayName="accesoryList">
<div *ngFor="let accesory of accesoryList; index as index">
<label>
<input type="checkbox" [formControlName]="index" /> {{ accesory.name }}
</label>
</div>
</div>
你可以在 https://stackblitz.com/edit/angular-2sixzt?file=src%2Fmain.ts 上查看一个示例,演示了如何使用它。
英文:
You are using a formArray
in a wrong way. You should use formArrayName
and index
in the template and also need to add formControl
when you get the list from the backend.
So the code should be like in TS
const accesoryListControl = this.form.get('accesoryList') as FormArray;
this.accesoryList.forEach((accesory) => {
accesoryListControl.push(new FormControl(accesory.checked));
});
and in HTML
<div formArrayName="accesoryList">
<div *ngFor="let accesory of accesoryList; index as index">
<label>
<input type="checkbox" [formControlName]="index" /> {{ accesory.name }}
</label>
</div>
</div>
I created an example at https://stackblitz.com/edit/angular-2sixzt?file=src%2Fmain.ts how to use it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论