How to check if value is number

后端 未结 3 677
陌清茗
陌清茗 2021-01-04 04:24

I have this function :

 $scope.SearchTicketEvent = function (ticketPinOrEvent)
            {
                if (ticketPinOrEvent != undefined)
                      


        
相关标签:
3条回答
  • 2021-01-04 05:07

    In Angular 6 this works without using any prefix.

    Example:

    if(isNumber(this.YourVariable)){
        // your Code if number
    }
    else {
        // Your code if not number
    }
    
    0 讨论(0)
  • 2021-01-04 05:20

    If you want to use angular.isNumber

        if ( !isNaN(ticketPinOrEvent) && angular.isNumber(+ticketPinOrEvent)) {
        }
    
    0 讨论(0)
  • 2021-01-04 05:25

    You might use the typeof to test if a variable is number.

    if (typeof ticketPinOrEvent === 'number') {
        $scope.PinTicketSearch(ticketPinOrEvent);
    }
    

    Or might try this:

    if (!isNaN(ticketPinOrEvent) && angular.isNumber(ticketPinOrEvent)) {
        $scope.PinTicketSearch(ticketPinOrEvent);
    }
    

    Testing against NaN:

    NaN compares unequal (via ==, !=, ===, and !==) to any other value -- including to another NaN value. Use Number.isNaN() or isNaN() to most clearly determine whether a value is NaN. Or perform a self-comparison: NaN, and only NaN, will compare unequal to itself.

    0 讨论(0)
提交回复
热议问题