angular2 dynamic form calculate amount total

后端 未结 2 1907
小蘑菇
小蘑菇 2021-01-16 12:52

Trying to calculate a total payOffs.amount (payOffs is a FormArray). Not sure how to do it properly so the total would observe changes to current and new for amounts. Here i

2条回答
  •  渐次进展
    2021-01-16 13:39

    I added a function:

    // calculate a sum of all payoffs
    sumPayOffs() {
      return this.myModel.payOffs.reduce((sum, val) => sum + val.amount, 0);
    }
    

    and in HTML (just before "Add Pay Off" button) I added readonly number field, displaying the sum:

    
      
    
    
      
    
    

    I changed pay offs field type from text to number, so inserted into mode value will be numeric.

    Updated plunker: http://plnkr.co/edit/Q5HYcpnPQosSYvk2KkoP?p=preview

提交回复
热议问题