简体   繁体   English

AngularJS。 相等运算符为false时返回true

[英]AngularJS. Equality operator returns true when false


Have an equality operator in angularJS that is not returning as I should. 在angularJS中有一个等于运算符,但没有按我的要求返回。

example: 例:
The value that comes from the object "data.valuea" is 50. 来自对象“ data.valuea”的值为50。
Have the value that comes from the object "data.valueb" is 200 来自对象“ data.valueb”的值是200

<p ng-show="{{data.valuea >= data.valueb}}" class="ng-hide premioprogress">OK</p>
<p ng-show="{{data.valuea < data.valueb}}" class="ng-hide premioprogress">Faltam: {{data.valueb - data.valuea}} pts</p>

Am I doing something wrong? 难道我做错了什么? Or would a more correct way to do this? 还是会有更正确的方法来做到这一点?

It's likely that the values are actually strings and not numbers, so JavaScript is doing a string comparison. 这些值实际上可能是字符串,而不是数字,因此JavaScript正在进行字符串比较。 One way to get around this is to put parseInt() on the scope and use it in your expression... 解决此问题的一种方法是将parseInt()放在作用域上,并在表达式中使用它。

$scope.parseInt = function (i) {
    return parseInt(i, 10);
};

<p ng-show="parseInt(data.valuea) >= parseInt(data.valueb)"></p>

JSFiddle JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM