角度计算奇怪的行为

时间:2016-03-30 08:07:52

标签: javascript css angularjs

我使用angular.js计算高度并将值添加到css样式标记中,它在ng-repeat循环中

使用此行计算错误:

<div style="height: {{60.0 / 100.0 * (100 - (100.0 / 0.27 * (item.wert + 1.0 - 0.95)))}}%; ">
    <p>{{item.wert}}</p>
</div>

当我将item.wert与因子1相乘时,结果是正确的, 所以这有效:

<div style="height: {{60.0 / 100.0 * (100 - (100.0 / 0.27 * ((item.wert * 1.0) + 1.0 - 0.95)))}}%; ">
    <p>{{item.wert}}</p>
</div>

有谁知道为什么我必须将它乘以1? 谢谢!

2 个答案:

答案 0 :(得分:1)

我认为* 1是一个字符串而不是一个int,所以当你执行Sprite时,它实际上将它变成一个int,你可以用它来计算。

答案 1 :(得分:0)

item.wert的类型取决于其中存储的值。例如,代码打印string

$scope.item={};
$scope.item.wert = "";
console.log(typeof($scope.item.wert)) //string;  

但是关注代码打印编号:

$scope.item={};
$scope.item.wert = 2;
console.log(typeof($scope.item.wert)) //number;

因此,您的表达结果可能会因item.wert中存储的值而异。

Please have a look at the Arithmetic operators article of MDN.

加法(+):

// Number + String -> concatenation
5 + "foo" // "5foo"

减法( - ):

 // Number - String -> NaN
    "foo" - 3 // NaN

乘法(*)

// Number * String -> NaN
"foo" * 2 // NaN

希望以上信息能够回答您的问题。

相关问题