Angular 发送表格中表格行的所有字段,表格以表格形式包装-角度
我在Angular中有一个Angular表,目前我在提交时发送id_编号,但我想做的是发送表行中的所有数据?到目前为止,我的代码是: html文件Angular 发送表格中表格行的所有字段,表格以表格形式包装-角度,angular,Angular,我在Angular中有一个Angular表,目前我在提交时发送id_编号,但我想做的是发送表行中的所有数据?到目前为止,我的代码是: html文件 <form [formGroup]="assetForm" (ngSubmit)="onSubmit()"> <table class="table table-striped table-hover mb-10"> <thead> <tr>
<form [formGroup]="assetForm" (ngSubmit)="onSubmit()">
<table class="table table-striped table-hover mb-10">
<thead>
<tr>
<th>Number</th>
<th>Sev</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let incident of data">
<td>
<label class="form-radio">
<input type="radio" name="id_number" [value]="incident.number" formControlName="id_number" />
<i class="form-icon"></i>{{incident.number}}
</label></td>
<td>{{incident.sev}}</td>
<td>{{incident.phone}}</td>
</tr>
</tbody>
</table>
<button class="btn btn-primary" [disabled]="!Form.valid" type="submit">Select</button>
</form>
ngOnInit() {
this.assetForm = new FormGroup({
id_number: new FormControl(''),
});
}
onSubmit() {
if (this.assetForm.invalid) {
this.assetForm.setErrors({ ...this.assetForm.errors, 'required': true });
return;
}
this.uploading = true;
this.service.post(this.assetForm.value).subscribe((response: any) => {
console.log(response);//On success response
}, (errorResponse: any) => {
console.log(errorResponse); //On unsuccessful response
this.error = true;
this.uploading = false;
});
}
<input type="radio" name="id_number" [ngValue]="incident" formControlName="id_number" >
你有两个选择
选项一
<form [formGroup]="assetForm" (ngSubmit)="onSubmit()">
<table class="table table-striped table-hover mb-10">
<thead>
<tr>
<th>Number</th>
<th>Sev</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let incident of data">
<td>
<label class="form-radio">
<input type="radio" name="id_number" [value]="incident.number" formControlName="id_number" />
<i class="form-icon"></i>{{incident.number}}
</label></td>
<td>{{incident.sev}}</td>
<td>{{incident.phone}}</td>
</tr>
</tbody>
</table>
<button class="btn btn-primary" [disabled]="!Form.valid" type="submit">Select</button>
</form>
ngOnInit() {
this.assetForm = new FormGroup({
id_number: new FormControl(''),
});
}
onSubmit() {
if (this.assetForm.invalid) {
this.assetForm.setErrors({ ...this.assetForm.errors, 'required': true });
return;
}
this.uploading = true;
this.service.post(this.assetForm.value).subscribe((response: any) => {
console.log(response);//On success response
}, (errorResponse: any) => {
console.log(errorResponse); //On unsuccessful response
this.error = true;
this.uploading = false;
});
}
<input type="radio" name="id_number" [ngValue]="incident" formControlName="id_number" >
在将数据发布到服务之前,在组件中的
onSubmit()
中需要查找所选事件。就像这样:
const selectedIncident = this.data
.find((incident) => incident.number === this.assetForm.get('id_number').value);
然后将其发送到服务:
this.service.post(selectedIncident).subscribe((response: any) => {
console.log(response);//On success response
}, (errorResponse: any) => {
console.log(errorResponse); //On unsuccessful response
this.error = true;
this.uploading = false;
});
希望我能帮助你:)