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
  • 16,580
  • 5
  • 54
  • 111
Sebastien
  • 6,640
  • 14
  • 57
  • 105

3 Answers3

36

Add to your scope:

$scope.isNumber = angular.isNumber;

and then use:

ng-show="isNumber(10)"

http://jsfiddle.net/88wqe/

CD..
  • 72,281
  • 25
  • 154
  • 163
  • 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,608
  • 5
  • 34
  • 36
-2

Try this:

ng-show='isNumeric(10)'
Mik378
  • 21,881
  • 15
  • 82
  • 180
  • 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