angular - 动态表单,提交时动态添加的行为空
问题描述
目前我正在研究性别概述,您可以在其中编辑性别,使用简单的表格添加或删除它。它已经可以添加和删除行。目前我正在努力在文本框中显示正确的 web api 数据作为值,并从表单中获取所有数据以将其发布回 web api。我的问题是,在我 console.log 更新数据源的数据后,新值仍然为空。我将在下面的屏幕截图中显示它。
这是我的桌子:
<div class="controlpanel">
<div id="operatoren">
<button mat-mini-fab color="primary"(click)="addRow()" > <mat-icon color="white"> add </mat-icon> </button>
</div>
</div>
<div class="container">
<form [formGroup]="genderForm">
<table mat-table [dataSource]="genderDataSource" #genderTable >
<ng-container matColumnDef="edit">
<th mat-header-cell *matHeaderCellDef> Speichern <mat-icon color="primary"> save </mat-icon> </th>
<td mat-cell *matCellDef="let anreden; ">
<button mat-mini-fab color="primary" (click)="saveRow()" > <mat-icon color="white"> save </mat-icon> </button>
</td>
</ng-container>
<ng-container matColumnDef="delete">
<th mat-header-cell *matHeaderCellDef> Löschen <mat-icon color="warn"> clear </mat-icon> </th>
<td mat-cell *matCellDef="let anreden; let i = index">
<button mat-mini-fab color="warn" (click)="confirmDelete(anreden.bezeichnung,i)" > <mat-icon color="white"> clear </mat-icon> </button>
</td>
</ng-container>
<ng-container matColumnDef="bezeichnung">
<th mat-header-cell *matHeaderCellDef> Bezeichnung </th>
<td mat-cell *matCellDef="let anreden; ">
<ng-container>
<mat-form-field appearance="standard">
<input matInput type="text" [value]='anreden.bezeichnung' formControlName="bezeichnung" name="anreden">
</mat-form-field>
</ng-container>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
</form>
</div>
这是我处理桌子的 ts
public displayedColumns = ['edit', 'delete', 'bezeichnung'];
public genderDataSource: any[];
public anreden = [] as any;
genderForm: FormGroup;
constructor(
public dialog: MatDialog,
public aService: AnredeVorschlägeService,
private fb: FormBuilder,
) {
}
createNewGender(): FormGroup{
return this.fb.group({
anredeVorschlagId: '',
bezeichnung: new FormControl(''),
})
}
@ViewChild('genderTable') table: MatTable<any>;
ngOnInit(): void {
this.genderForm = this.fb.group({
anredeVorschlagId: '',
bezeichnung: [''],
});
//Alle bezeichnungn
this.aService.getAnrede()
.subscribe(
data => {
this.anreden = data;
this.genderDataSource = data;
console.log(this.genderDataSource);
this.getStandardValues();
});
}
addRow(){
this.genderDataSource.push(this.createNewGender().value);
this.table.renderRows();
}
saveRow(){
console.log(this.genderForm.value);
console.log(this.genderDataSource);
}
getStandardValues(){
this.genderForm.patchValue({
bezeichnung: this.anreden
})
}
delRow(i:any){
this.genderDataSource.splice(i, 1);
this.table.renderRows();
}
confirmDelete(bezeichnung: string, i: any){
this.dialog.open(ConfirmDialogComponent).afterClosed().subscribe(confirm => {
if(confirm){
this.delRow(i);
}
})
}
}
第一行和第二行应该有来自 web api 的值“Herrn”和“Frau”,我不知道为什么它们有“object Object”。第三行是我动态添加的行。在我的数组中,有 3 个条目应该是“bezeichnung:123”而不是“bezeichnung:''”(第 56 行)。否则只需记录表单值即可,并返回正确的值(第 55 行)。
我的服务:
readonly aV = 'http://localhost:5000/api/anredevorschläge';
getAnrede(): Observable<AnredeVorschläge[]> {
return this.http.get<AnredeVorschläge[]>(this.aV);
}
解决方案
首先,如果您可以在示例中添加一些类型信息,那就太好了。
其次,我认为您的表单结构必须更改。目前,您的所有行都与genderForm
您实际为此行创建的表单组连接。删除genderForm
并改为执行以下操作以实际始终在正确的表单控件上工作。
<ng-container>
<mat-form-field appearance="standard" [formGroup]=anrede.parent>
<input matInput type="text" formControlName="bezeichnung">
</mat-form-field>
</ng-container>
第三,API 中表单控件的问题似乎[object object]
来自AnredeVorschlägeService.getAnreden()
函数,因为您直接将此函数的结果分配给数据源。为了解决这个问题,我们需要这个函数的代码或函数的返回类型getAnreden()
。
推荐阅读
- mysql - 有没有一种简单的方法来检查字符串中的大写字符?
- javascript - 使用 NodeJS 和 FS 在 JS 文件末尾添加换行符
- java - 如何排除因子程序中的数字?AND 如何在输入某个数字之前重新提示用户?
- django - 未设置 Django 模板变量
- python - 无法将代码格式化为所需的输出
- r - 通过插值将带有分数的数字数据帧转换为 r 中的整数
- html - 苗条的高架床
- acumatica - 当我覆盖“NoteID”基本屏幕标题表字段(位置<==表)时。为什么我收到“指定的演员表无效”错误
- angular - ngrx 无状态组件 + 与 store 交互
- html - 如何在 Angular 9 中让视频在鼠标悬停时播放并在鼠标悬停时暂停?