【发布时间】:2020-06-30 18:59:20
【问题描述】:
我正在尝试提交来自表单数组的值。但是它不起作用......这是代码
<form *ngIf="incomeForm" [formGroup]="incomeForm" (ngSubmit)="onSubmitForm()">
<table class="table table-border">
<thead>
<th>Description</th>
<th>somme</th>
</thead>
<tbody>
<ng-container formArrayName="budgetIncomes" *ngFor="let item of incomesArray.controls; let i=index">
<tr *ngIf="item.get('isEditable').value" [formGroupName]="i">
<td><input type="text" ngModel formControlName="label"></td>
<td><input type="number" ngModel formControlName="somme"
[ngStyle]="{'color': item.get('somme').value >=0? 'green' : 'red'}"></td>
<button (click)="doneRow(item)">done</button>
</tr>
<tr *ngIf="!item.get('isEditable').value">
<td>{{item.get('label').value}}</td>
<td>{{item.get('somme').value}}</td>
<button (click)="editRow(item)">edit</button>
</tr>
</ng-container>
</tbody>
</table>
<div class="action-container">
<button class="add" (click)="addIncome()">Add Income</button>
</div>
<button type="submit" class="btn btn-primary">Enregistrer</button>
</form>
<p class="result" [ngStyle]="{'color': getSum()>=0? 'green' : 'red'}">{{getSum()}}</p>
onSubmitForm() {
const formValue = this.incomeForm.value;
const newBudget = new Budget(
formValue['label'],
formValue['somme']
)
this.budgetService.validateBudget(newBudget);
}
newBudget 的值似乎是未定义的,但我不明白为什么?
有什么想法吗?
提前谢谢! :)
【问题讨论】:
-
您能展示一下您是如何构建表单的吗