【问题标题】:How to parseInt in angularjs expression如何在angularjs表达式中解析Int
【发布时间】:2017-02-07 11:05:10
【问题描述】:

我有这种情况:

<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 &gt; 1200

如何在 AngularJS 表达式中parseInt()

【问题讨论】:

  • name.avalue in ng-if 它不应该是 name.value 就像你的例子一样吗?
  • 使用简单的Number(name.value)

标签: angularjs expression parseint


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      如果您想在视图中使用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>
      

      【讨论】:

        【解决方案4】:

        你也可以试试:

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

        【讨论】:

          猜你喜欢
          • 2014-02-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多