【发布时间】:2022-01-23 15:27:36
【问题描述】:
所以我有这个要求,我正在尝试以表格的形式提交一个由五条记录组成的表格。这就是它的样子 table
这是对应的代码:
<form [formGroup]="FeedBack" (ngSubmit)="ADDFeedback()">
<table class="form-group">
<tr>
<th> Section </th>
<th> Q.No </th>
<th> Question Description </th>
<th> Answer_ShortTxt. </th>
<th> Answer_longTxt. </th>
<th> Comments </th>
</tr>
<tbody>
<tr *ngFor="let obj of QuestionsForSubmittedAnswersArray">
<td ><input type="textarea" placeholder={{obj.Section}} [value]="obj.Section" class="form-control" id="Section" formControlName="Section"></td>
<td><input type="text" placeholder={{obj.QuestionIDId}} [value]="obj.QuestionIDId" class="form-control" id="QuestionID" formControlName="QuestionID"></td>
<td><input type="text" placeholder={{obj.question}} [value]="obj.question" class="form-control" id="question" formControlName="question"></td>
<td><input type="text" placeholder={{obj.Answer_ShortTxt}} [value]="obj.Answer_ShortTxt" class="form-control" id="Answer_ShortTxt" formControlName="Answer_ShortTxt"></td>
<td><input type="text" placeholder={{obj.Answer_LongTxt}} [value]="obj.Answer_LongTxt" class="form-control" id="Answer_LongTxt" formControlName="Answer_LongTxt"></td>
<td><input type="text" class="form-control" id="Feedback" formControlName="FeedBack"></td>
</tr>
</tbody>
</table>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
.ts 文件
import { ThisReceiver } from '@angular/compiler';
import { FormBuilder,FormGroup,Validators,FormControl } from '@angular/forms';
import { Component, OnInit } from '@angular/core';
import { SharepointserviceService } from 'src/Service/sharepointservice.service';
// import { resolve } from 'dns';
// import { SharepointserviceService } from 'src/Service/sharepointservice.service';
@Component({
selector: 'app-business-buendorsement-form',
templateUrl: './business-buendorsement-form.component.html',
styleUrls: ['./business-buendorsement-form.component.css']
})
export class BusinessBUEndorsementFormComponent implements OnInit {
// private service:SharepointserviceService
SubmittedAnswers:any[]=[];
QuestionsAndAnswers:any[]=[];
QuestionsForSubmittedAnswersArray:any[]=[];
FeedBack!:FormGroup;
constructor(private service:SharepointserviceService,private fb:FormBuilder) {
this.FeedBack=fb.group({
Section:new FormControl(),
QuestionID:new FormControl(),
question:new FormControl(),
Answer_ShortTxt:new FormControl(),
Answer_LongTxt:new FormControl(),
FeedBack:new FormControl()
})
}
我无法捕捉输入字段的默认值,它提交为 null 并且单击提交时,所有五条记录都应该被控制台记录,但我只能获得第一条。 结果/输出:
【问题讨论】:
-
您应该为这种类型的表单使用 FormArray。请查看angular.io/api/forms/FormArray
-
如果可以请发送 stalkblitz,我可以帮忙吗?
标签: html angular typescript