首页 > 解决方案 > 我想通过选择多个值作为表 2 中提到的对象将数据发送到后端

问题描述

我想实现格式 2 的输出,我只得到格式 1。所以请帮助我。我怎样才能做到这一点?请帮助我。我是角度和堆栈溢出的新手。如果我犯了任何愚蠢的错误,请不要对我苛刻。谢谢你。谢谢你。我想实现格式 2 的输出,我只得到格式 1。所以请帮助我。我怎样才能做到这一点?请帮助我。我是角度和堆栈溢出的新手。如果我犯了任何愚蠢的错误,请不要对我苛刻。谢谢你。谢谢你。

format:1(present output)

"subjectList": [
{
"subject": [
{
"subjectId": 1,
"subject": "English"
}
]
}
]

format:2(desired output)   
{

"subjectList": [
{

"subjectId": 1,
"subject": "English"
},
{

"subjectId": 1,
"subject": "English"
}    
]
}

这是我的 ts 文件。

buildteacherForm() {             

this.teacherForm = this.fb.group({
teacherId: [this.teacher.teacherId],
name: [this.teacher.name, [Validators.required, 
Validators.minLength(2), Validators.maxLength(20)]],
qualification: [this.teacher.qualification, [Validators.required, 
Validators.minLength(2), Validators.maxLength(20)]],
address: [this.teacher.address, [Validators.required, 
Validators.minLength(2), Validators.maxLength(20)]],
phoneNo: [this.teacher.phoneNo, [Validators.required, 
Validators.minLength(10)]],
dob: [this.teacher.dob, [Validators.required]],
subjectList: this.fb.array([]),

});

this.addSubjects();
}

addSubjects() {
const subjects = this.teacherForm.controls.subjectList as FormArray;
subjects.push(this.fb.group({
subject: [this.teacherSubject.subject],

}));
}

HTML: here is my html part

<div class="col-md-4">
<div
*ngFor="let teacherSubject of 
teacherForm.get('subjectList').controls;index as i"            
formArrayName="subjectList">
<div [formGroupName]="i">
<kurumba-form-group>
<mat-form-field class="example-full-width">
<mat-label>Subject list</mat-label>
<mat-select
multiple
matInput
id="subject"
formControlName="subject"
required>
<mat-option disabled> Select subject</mat-option>
<mat-option
[value]="subject"
*ngFor="let subject of teacherSubjectList"
>{{ subject.subject }}</mat-option>
</mat-select>
<mat-hint align="start">Select subject</mat-hint>
</mat-form-field>
</kurumba-form-group>
</div>
</div>
</div>

标签: angular-materialangular9formarrayangular2-formbuilderangular-material-8

解决方案


推荐阅读