2016-12-30 26 views
1

我想添加工作項目成本,但它顯示原始值。如何在Angularjs中添加項目成本?

例子:

item[1].cost =2 ,item[2].cost = 2 .. 

當我添加第三個item[3].cost = 8這是Total = 228。我想添加SUM,如何做到這一點?我要總共=12

addWorkItem() { 

    this.current_job.work_items.push(this.current_workitem); 
    this.Total = 0; 

    for (var i = 0, len = this.current_job.work_items.length; i < len; i++) { 
     this.Total += this.current_job.work_items[i].cost; 
    } 
    this.current_workitem = {}; 

    } 

See picture

+3

很難說沒有看到更多的代碼,但看起來你應該叫parseFloat上this.current_job.work_items [I]。成本計算this.Total –

+0

什麼時候不工作?你對Total有什麼價值? – RamblinRose

回答

2

那是因爲你是串聯的數字,而不是增加。

addWorkItem() { 
    this.current_job.work_items.push(this.current_workitem); 
    this.Total = 0; 

    for (var i = 0, len = this.current_job.work_items.length; i < len; i++) { 
    this.Total += parseFloat(this.current_job.work_items[i].cost); 
    } 
    this.current_workitem = {}; 

} 
+0

謝謝...... :) –

+0

你也可以在你的輸入中使用'type =「number」',它會將它們轉換爲數字,並避免解析可能返回'NaN'的浮點數。 – TheSharpieOne