算术加法(+)不NG绑定工作 [英] Arithmetic addition (+) is not working in ng-bind
问题描述
有是具有对象
objData.pendingCount = '2',objData.refundCount = '1',objData.saleCount = 43;
我执行
<td ng-bind="(objData.pendingCount / objData.saleCount * 100).toFixed(2)"></td>
这是工作正常,但是当我做
this is working fine but when I do
<td ng-bind="(objData.pendingCount +objData.refundCount)/ objData.saleCount * 100).toFixed(2)"></td>
下面+不执行算术调度研究,而这是concating。我怎样才能做到这一点?
Here + is not performing arithmetic opration rather it is concating. How can I achieve this ?
推荐答案
我认为这些价值来自于某种形式的文本框(通过NG-模型)。如果是这样,解决这一问题最简单的方法是设置一个他们键入='号'
和角度会采取其他的事情!
I assume that these values come from a text box of some sort (via ng-model). If so, the easiest way to fix this would be to set a type='number'
on them and angular will take care of the rest!
<input ng-model="objData.pendingCount" type="number" />
...
另一种方法是简单地preFIX +强制数字转换,像这样
The alternative is to simply prefix + to force a number conversion, like so
<span ng-bind="((+objData.pendingCount + +objData.refundCount)/ objData.saleCount * 100).toFixed(2)"></span>
为什么不能使用数字(...),以使字符串一个数字在角前pression
使用编号
字符串转换为数字不会直接因为(从的 https://docs.angularjs.org/guide/ex$p$pssion )
Using Number
to convert the string to a number will not work directly because (from https://docs.angularjs.org/guide/expression)
环境:JavaScript的前pressions是针对全球评估
窗口。在棱角分明,前pressions是针对一个评估范围的对象。
Context: JavaScript expressions are evaluated against the global window. In Angular, expressions are evaluated against a scope object.
所以,如果你需要使用编号
,你可以这样做
So if you need to use Number
you can do something like
$scope.Number = $window.Number;
在您的控制器在你的前pression使用它之前。这同样适用于全球其他的窗口属性。
in your controller before you use it in your expression. The same goes for other global window properties.
这篇关于算术加法(+)不NG绑定工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!