Ng show if the variable is a number

19,000

Solution 1

Add to your scope:

$scope.isNumber = angular.isNumber;

and then use:

ng-show="isNumber(10)"

http://jsfiddle.net/88wqe/

Solution 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.

Share:
19,000
Sebastien
Author by

Sebastien

I am a freelancer working on your projects using this stacks : - Magento 1 &amp; 2 - Angular js 1&amp;2 - Ruby on rails I made this websites : https://www.digimage.fr (Magento 2) http://www.johngalliano.fr (Magento 2) http://www.paycar.fr (Ruby on rails) I work with customer of any size, improve an existing project or create a product for a startup. You can find all my work on my website : http://www.sebfie.com

Updated on June 05, 2022

Comments

  • Sebastien
    Sebastien about 2 years

    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)'
  • Sean the Bean
    Sean the Bean over 9 years
    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 stackoverflow.com/questions/18082.
  • Kiarash
    Kiarash almost 8 years
    Does this works for string input? I dont think so. This just checks the type of the parameter you passing into