如何在 angularjs 表达式中解析 Int



我有这样的条件:

<td ng-repeat="sal in name.salaries" 
    ng-if="name.avalue>sal.annual_value">
       Value must be less than or equal to Balance Amount
</td>

真实场景中发生的事情是

例如。 name.value=1200 sal.annual_value=42因为它不是解析,这就是为什么它考虑42 > 1200.

如何在 AngularJS 表达式中parseInt()

我会使用范围函数进行验证:

{...}
scope.checkSalaries = function(avalue, annual_value) {
   return avalue > annual_value;
}
{...}

在 HTML 中:

<td ng-repeat="sal in name.salaries"
    ng-if="checkSalaries(name.avalue,sal.annual_value)">
       Value must be less than or equal to Balance Amount
</td>
您可以在

ng-if中使用parseInt

<td 
    ng-repeat="sal in name.salaries"
    ng-if="parseInt(name.avalue)>parseInt(sal.annual_value)">
        Value must be less than or equal to Balance Amount
</td>

或者,您可以创建一个函数来在控制器中检查这一点:

<td 
    ng-repeat="sal in name.salaries"
    ng-if="isGreater(name.avalue, sal.annual_value)">
        Value must be less than or equal to Balance Amount
</td>
$scope.isGreater = function(val1, val2) {
    return parseInt(val1) > parseInt(val2);
};
如果要

在视图中使用parseInt方法,可以在控制器中定义一个Number $scope,然后在视图中使用其内置方法parseInt()

例如

控制器:

$scope.number = Number;

视图:

<td ng-repeat="sal in name.salaries" ng-if="number.parseInt(name.avalue)>number.parseInt(sal.annual_value)">
Value must be less than or equal to Balance Amount</td>

你也可以试试:

<td 
    ng-repeat="sal in name.salaries"
    ng-show="isGreater(name.avalue, sal.annual_value)">
        Value must be less than or equal to Balance Amount
</td>
$scope.isGreater = function(val1, val2) {
    return parseInt(val1) > parseInt(val2);
};

最新更新