AngularJS数学加入字符串而不是添加

时间:2014-01-09 16:22:49

标签: javascript angularjs

我有一个标准$scope.totals = $scope.totals = {storage:0, dailystorage:0};和一个angular.forEach,可以cam.storage添加$scope.totals.storage给我总存储空间。

我用它来做到这一点:

$scope.totals.storage = $scope.totals.storage+cam.storage;

问题在于,如果两个cam.storage21.0915.82,则会$scope.totals.storage 21.0915.82 - 基本上将它们添加为字符串而不是喜欢数学。

如何将其作为补充 - 而不是加入?

3 个答案:

答案 0 :(得分:1)

如果它们是连接而不是添加,则听起来你需要将它们解析为小数(你也可以根据需要使用toFixed(int)来限制小数)。

$scope.totals.storage = parseFloat($scope.totals.storage)+parseFloat(cam.storage);

答案 1 :(得分:1)

根据您发布的内容判断($scope.totals已经是一个数字),cam.storage是一个字符串。在将其添加到现有值之前,您需要将其解析为数字:

$scope.totals.storage += parseFloat(cam.storage);

答案 2 :(得分:-1)

我的解决方案我使用{{(a * 1)+(b * 1)}}它有效。