gpt4 book ai didi

angular - 在新选项选择错误时使用多项选择的 react 形式

转载 作者:行者123 更新时间:2023-12-04 01:36:27 26 4
gpt4 key购买 nike

是否有关于如何实现多选和响应式表单的示例?

我正在尝试在 multiselect-dropdown 上设置所选项目(从数据库中检索),它会更新显示的项目( View ),但会引发以下错误:

core.js:12501 ERROR TypeError: control.registerOnChange is not a function
at setUpModelChangePipeline (forms.js:2064)
at setUpControl (forms.js:2007)
atFormGroupDirective.push../node_modules/@angular/forms/fesm5/forms.js.FormGroupDirective.addControl
(forms.js:4969)
at FormControlName.push../node_modules/@angular/forms/fesm5/forms.js.FormControlName._setUpControl
(forms.js:5572)
at FormControlName.push../node_modules/@angular/forms/fesm5/forms.js.FormControlName.ngOnChanges
(forms.js:5491)
at checkAndUpdateDirectiveInline (core.js:18533)
at checkAndUpdateNodeInline (core.js:19801)
at checkAndUpdateNode (core.js:19763)
at prodCheckAndUpdateNode (core.js:20304)
at Object.eval [as updateDirectives] (AddAppointmentsComponent.ngfactory.js:646)

我在尝试选择新选项时遇到错误。

core.js:12501 ERROR Error: Cannot find form control at index 3
at FormArray.push../node_modules/@angular/forms/fesm5/forms.js.FormArray._throwIfControlMissing (forms.js:4001)
at forms.js:3870
at Array.forEach (<anonymous>)
at FormArray.push../node_modules/@angular/forms/fesm5/forms.js.FormArray.setValue (forms.js:3869)
at updateControl (forms.js:2059)
at MultiSelectComponent.onChangeCallback (forms.js:2044)
at MultiSelectComponent.push../node_modules/ng-multiselect-dropdown/fesm5/ng-multiselect-
dropdown.js.MultiSelectComponent.addSelected (ng-multiselect-dropdown.js:348)
at MultiSelectComponent.push../node_modules/ng-multiselect-dropdown/fesm5/ng-multiselect-
dropdown.js.MultiSelectComponent.onItemClick (ng-multiselect-dropdown.js:160)
at Object.eval [as handleEvent] (MultiSelectComponent.ngfactory.js:135)
at Object.handleEvent (core.js:19545)

数据

"questionnaireData":[{"questionnaireId":"Xvqo5atpw",

"timeRepeat":[
{"item_id":1,"item_text":"01:00"},
{"item_id":2,"item_text":"02:00"},
{"item_id":2,"item_text":"02:00"}
]
}],

在组件 formArray 中赋值给 formBuilder.array(ap.timeRepeat)

component.ts

this.addAppointmentForm.setControl('questionnaireData', 
this.setExistingQuestionnairesDetails(this.AppointmentList.questionnaireData));

setExistingQuestionnairesDetails(questionDataArray: IQuestionnaireData[]): FormArray {
const formArray = new FormArray([]);
console.log(questionDataArray);
questionDataArray.forEach(ap=>{

formArray.push(this.formBuilder.group({
questionnaireId: ap.questionnaireId,
timeRepeat: this.formBuilder.array(ap.timeRepeat),

}))
})
return formArray;
}

compnent.html

<ng-multiselect-dropdown formControlName="timeRepeat" [data]="dropdownList"
[(ngModel)]="timeRepeat" [settings]="dropdownSettings" (onSelect)="onItemSelect($event)"
(onSelectAll)="onSelectAll($event)" (onDeSelect)="onDeSelect($event)">
</ng-multiselect-dropdown>

请告诉我如何解决此错误或一些更好的方法来完成任务。

最佳答案

不要混合响应式(Reactive)和模板形式(不要用于同一输入的控件 [(ngModel)] 和 formControlName)。如果您使用控件,则无需使用(onDeSelect、onSelectAll 或 onSelect)。顺便说一句,一个 ng-multiselect-dropdown 存储一个数组。

是的,您不需要 FormArray,只需要一个 formControl(或者如果您使用 [(ngModel)] 一个独特的数组变量)。是的,FormControl 可以存储数组或对象

stackblitz中最简单的例子

我们有一个数据

  this.data = [
{ item_id: 1, item_text: 'Hanoi' },
{ item_id: 2, item_text: 'Lang Son' },
{ item_id: 3, item_text: 'Vung Tau' },
{ item_id: 4, item_text: 'Hue' },
{ item_id: 5, item_text: 'Cu Chi' }
];

表格是

   this.form = new FormGroup({
name: new FormControl([this.data[0],this.data[2]], Validators.required)
});

.html

<form [formGroup]="form" (ngSubmit)="save()" (reset)="resetForm()">

<ng-multiselect-dropdown #multiSelect
[placeholder]="'Select City'"
[data]="data"
formControlName="name"
[settings]="settings"
[disabled]="false"
>
</ng-multiselect-dropdown>

<span *ngIf="form.get('name')?.errors?.required" style="color: red;">
City is required
</span>

<br/><br/>

<input type="submit" value="Submit"> | <input type="reset" value="Reset">

</form>

关于angular - 在新选项选择错误时使用多项选择的 react 形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59369482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com