sus Sam
sus Sam

Reputation: 37

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

Here im having textBox in *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}}

Here im Getting OutPust as If i Enter12 Its Comming AS 121

Upvotes: 0

Views: 184

Answers (2)

Emmy
Emmy

Reputation: 3743

The reason this is happening is because values that come from an input field are always strings. So BillofQty[i] is a string. And using the + operator on a string results in the two values being concatenated. So you will first need to convert this value into a number.

If you don't care if it is saved as string or number you could simply put: {{+BillofQty[i]+1}} or {{Number(BillofQty[i])+1}}. This converts your string value in BillofQty[i] to a number.

If you want to save the value as a number directly you will probably have to split your [(ngModel)] into [value]="BillofQty[i]" and (ngModelChange)="yourMethod(i)" where you convert the value to a number in yourMethod and then save it to this.BillofQty[i].

Upvotes: 1

Anusha_Mamidala
Anusha_Mamidala

Reputation: 397

Try using

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

Upvotes: 1

Related Questions