Angular2 如何在 *ngFor 中添加两个值

Angular2 How can i add two values when its in *ngFor

这里我在 *ngFor="let abc in Apple;let i = index"

中有文本框
<tr  *ngFor="let abc in Apple;let i = index">

<td>        <input matInput  name="UnitofPrice"  [(ngModel)] = "BillofQty[i]">
</td>
<td>{{BillofQty[i]+1}}

我在这里得到 OutPut 就像我输入 12 它即将到来 AS 121

尝试使用

<td>
   {{BillofQty[i]*1 + 1*1}}
</td>

发生这种情况的原因是来自输入字段的值始终是字符串。所以 BillofQty[i] 是一个字符串。在字符串上使用 + 运算符会导致两个值连接起来。因此,您首先需要将此值转换为数字。

如果您不关心它是保存为字符串还是数字,您可以简单地输入: {{+BillofQty[i]+1}}{{Number(BillofQty[i])+1}}。这会将 BillofQty[i] 中的字符串值转换为数字。

如果您想直接将值保存为数字,您可能必须将 [(ngModel)] 拆分为 [value]="BillofQty[i]"(ngModelChange)="yourMethod(i)",然后在您的方法中将值转换为数字然后保存到 this.BillofQty[i].