10

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)'
shA.t
  • 15,880
  • 5
  • 49
  • 104
Sebastien
  • 6,560
  • 14
  • 54
  • 102

3 Answers3

36

Add to your scope:

$scope.isNumber = angular.isNumber;

and then use:

ng-show="isNumber(10)"

http://jsfiddle.net/88wqe/

CD..
  • 68,981
  • 24
  • 147
  • 156
  • 1
    Does this works for string input? I dont think so. This just checks the type of the parameter you passing into – Kiarash Sep 21 '16 at 23:32
4
<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.

Community
  • 1
  • 1
eddiec
  • 7,588
  • 5
  • 33
  • 36
-2

Try this:

ng-show='isNumeric(10)'
Mik378
  • 21,449
  • 13
  • 75
  • 170
  • Please link to documentation for `isNumeric`, if it exists. I don't believe it's native to JS or to Angular, given discussions such as http://stackoverflow.com/questions/18082. – Sean the Bean Mar 23 '15 at 20:02