【发布时间】:2019-07-14 04:58:55
【问题描述】:
如何获得输入字段中两个值的总和并将结果存储在另一个输入字段中?
换句话说,这两个值value1 和value2 已经有它们与 *ngFor 但我不能做加法,而且当任何值改变时,结果都会被修改。
component.html
<mat-form-field appearance="outline" class="col-md-3">
<mat-label>Precio regular</mat-label>
<input matInput type="number" formControlName="precioRegular" value="{{paquete.precio}}" (change)="updateTotal(paquete.precio)">
</mat-form-field>
<mat-form-field appearance="outline" class="col-md-3">
<mat-label>Descuento</mat-label>
<input matInput type="number" formControlName="descuento" value="{{paquete.descuento}}" (change)="updateTotal(paquete.descuento)">
</mat-form-field>
<mat-form-field appearance="outline" class="col-md-3">
<mat-label>Precio Final</mat-label>
<input matInput formControlName="precioFinal" [value]="total">
</mat-form-field>
组件.ts
total: number;
this.forma = this.fb.group({
precioRegular: [ ],
descuento: [ ],
precioFinal: [ ],
});
updateTotal(item) {
if (item) {
this.total += item.value;
} else {
this.total -= item.value;
}
// console.log(this.total);
}
【问题讨论】:
-
似乎不是反应式表单的理想用例。您应该改用模板驱动的表单。
-
输入表单元素用于收集...来自用户的输入。不存储计算的输出。为什么将输出存储在输入中?
-
因为用户输入的新数据,以后会存入数据库
标签: angular typescript