我正在使用Angular 4.0.2构建一个应用程序.如何向表单添加按钮以添加新行输入和删除特定行的删除按钮?我的意思是我想要一个像这样的表格.我希望我的表单看起来像这样:
.
这是我的代码:
附加invoice.component.html
<h3 class="page-header">Add Invoice</h3>
<form [formGroup]="invoiceForm">
<div formArrayName="itemRows">
<div *ngFor="let itemrow of itemRows.controls; let i=index" [formGroupName]="i">
<h4>Invoice Row #{{ i + 1 }}</h4>
<div class="form-group">
<label>Item Name</label>
<input formControlName="itemname" class="form-control">
</div>
<div class="form-group">
<label>Quantity</label>
<input formControlName="itemqty" class="form-control">
</div>
<div class="form-group">
<label>Unit Cost</label>
<input formControlName="itemrate" class="form-control">
</div>
<div class="form-group">
<label>Tax</label>
<input formControlName="itemtax" class="form-control">
</div>
<div class="form-group">
<label>Amount</label>
<input formControlName="amount" class="form-control">
</div>
<button *ngIf="i > 1" (click)="deleteRow(i)" class="btn btn-danger">Delete Button</button>
</div>
</div>
<button type="button" (click)="addNewRow()" class="btn btn-primary">Add new …Run Code Online (Sandbox Code Playgroud) 我有我的反应形式,看起来像这样
<form [formGroup]="addInvoiceForm" (submit)="onAddInvoiceFormSubmit()">
<div [formArrayName]="itemRows">
<div *ngFor="let itemrow of addInvoiceForm.controls.itemRows.controls; let i=index" [formGroupName]="i">
<input autocomplete="off" type="text" formControlName="itemqty">
<input autocomplete="off" type="text" formControlName="itemrate">
<!-- the input field below is to be summed -->
<input autocomplete="off" type="text" formControlName="itemamt" [ngModel]="itemrow.get('itemqty').value * itemrow.get('itemrate').value">
</div>
</div>
<button type="button" (click)="addItemRow()">Add New Row</button>
</form>
Run Code Online (Sandbox Code Playgroud)
我想对用户创建的所有行的 itemamt 求和并将它们传递到表单数据中。有人可以帮助我,告诉我如何对表单中的所有 itemamt 字段求和并并排显示用户的总金额是多少?