2017-11-06 2 views
0

私は、ユーザーに請求書を提示するアプリを持っていますが、請求書に記載されている項目の合計値を除いてすべて設定されています。合計、私は連結値にionic2 angular2列の値を合計

this.http.post('http://myurl',data,headers) 
.map(res => res.json()) 
.subscribe(res => { 
console.log(res) 
loader.dismiss() 
this.items=res.server_response; 

    this.totalAmount = this.getTotal(); 


}); 
}); 
} 
; 
} 

getTotal() { 
    let total = 0; 
    for (var i = 0; i < this.items.length; i++) { 
     if (this.items[i].amount) { 
      total += this.items[i].amount; 
      this.totalAmount = total; 
     } 
    } 
    return total; 
} 

を得た表示値が(+ 4000 1000年+ 3500)であると仮定何のため0100035004000ある+を追加することにより、数に

+0

私があなたに提供した答えは、[this link](https://stackoverflow.com/questions/14667713/typescript-converting-a-string-to-a-number)に基づいています。これは何が起こっているのかを説明するのに役立ちますに。 –

+1

[TypeScriptの文字列を数値に変換する]の可能な複製(https://stackoverflow.com/questions/14667713/typescript-converting-a-string-to-a-number) –

答えて

0

解析items.amount:

getTotal() { 
let total = 0; 
for (var i = 0; i < this.items.length; i++) { 
    if (this.items[i].amount) { 
     total += +this.items[i].amount; 
     this.totalAmount = total; 
    } 
} 
return total; 
} 
0

total += this.items[i].amount;の代わりに数字を文字列として連結するように見える場合は、total += Number(this.items[i].amount);に変更して、文字列の文字列をtypecriptに変換します。

関連する問題