Ng show if the variable is a number

余生颓废 提交于 2019-12-06 20:24:31

问题


I want to display an element only if a value is a number. How to do this? I tried :

  • ng-show='angular.isNumber(10)'
  • ng-show='isNumber(10)'

回答1:


Add to your scope:

$scope.isNumber = angular.isNumber;

and then use:

ng-show="isNumber(10)"

http://jsfiddle.net/88wqe/




回答2:


<div ng-controller="Ctrl" ng-show="isNumber(num)">
    AAA
</div>

function Ctrl($scope){
    $scope.num = '10';

    $scope.isNumber= function (n) {
      return !isNaN(parseFloat(n)) && isFinite(n);
    }
}

Here's a JS Fiddle http://jsfiddle.net/7RD47/

It uses the top answer from Validate decimal numbers in JavaScript - IsNumeric(), which is a more complete way of validating numbers.




回答3:


Try this:

ng-show='isNumeric(10)'


来源:https://stackoverflow.com/questions/20324352/ng-show-if-the-variable-is-a-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!