如何在 angularjs 表达式中解析 Int

How to parseInt in angularjs expression

我有这种情况:

<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 因为它不是 parseInt 这就是为什么它考虑 42 > 1200.

如何在 AngularJS 表达式中 parseInt()

您可以在 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);
};

我会使用作用域函数来进行验证:

{...}
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>

你也可以试试:

<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);
};

如果您想在视图中使用 parseInt 方法,您可以在控制器中定义一个 Number $scope,然后在查看。

例如

控制器:

$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>