AngularJS: Return function based on input type

Eric Scandora

I have an input box in my HTML file. If the user inputs a number I would like to return getZip() and if the user inputs a string I would like to return getCity(). Is this possible to do with a single input box or would I need to have one designated for type="text" and one designated for type="number"?

view.html:

<div ng-app="myApp" ng-controller="myCtrl">
  <form ng-submit="getData()">
  <input type="search" ng-model="location">
  <button>Get Weather</button>
</form>

myApp.js:

1st attempt:

$scope.getData = function() {
  switch (angular.isString($scope.location)) {
    case true:
      return $scope.getCity();
      break;
    case false:
      return $scope.getZip();
      break;
    default:
      return "Location not recognized";
    };
  };

2nd attempt:

$scope.getData = function () {
  if (angular.isString($scope.location)) {
      return $scope.getCity() 
   } else {
      return $scope.getZip()
  };

It seems like even if the input is a number, once it is passed through the scope, it is a string no matter what. Is there a way around this? Thanks in advance!

Omri Aharon

You should check whether or not your input is a number, since all input would be of type string initially:

if (!isNaN($scope.location)) {
    return $scope.getZip()
}
else {
    return $scope.getCity()
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

TypeScript function return type based on input parameter

From Dev

Return delegate function based on type

From Java

Infer return type of function based on type guard

From Dev

Narrow return type based on input using generics

From Dev

function to return something based on input using R

From Dev

Calling angularjs function on text input based on length

From Dev

Arbitrary Function - Generate return type according to input

From Dev

C++ Function Return/Input Reference Type

From Dev

Casting Function Return Type Based on Class Field

From Java

Generic function where the return type depends on the input type in Scala?

From Dev

Typescript: Constrain function generic type based on the expected return type

From Dev

c++ return type of function to be a function of it's input

From Dev

C++ template metaprogramming - return a type based on runtime input

From Dev

C++ Return Type Deduction Based on Input Parameters

From Dev

How do I set input type based on variable in angularjs?

From Dev

How is HTML restricting text input based on the return value of a javascript function?

From Dev

Function return type mismatch phenomenon when returning different input ranges

From Dev

How to derive a column name in the return type from input parameters to the function?

From Dev

Function return type mismatch phenomenon when returning different input ranges

From Dev

How to derive a column name in the return type from input parameters to the function?

From Dev

How to infer return type of Promise based on function arguments?

From Dev

Python Typing: declare return value type based on function argument

From Dev

Kotlin: unchecked type inference based on function return value

From Dev

Typescript: Wrong overload selection based on function return type

From Dev

Input type number in AngularJS

From Dev

How can I type-hint a function where the return type depends on the input type of an argument?

From Dev

Is it possible to define a function return type based on a defined mapping from the type of a function argument?

From Dev

How can I write a function have a polymorphic return type based on the type argument of its type parameter?

From Dev

Call function based on input

Related Related

  1. 1

    TypeScript function return type based on input parameter

  2. 2

    Return delegate function based on type

  3. 3

    Infer return type of function based on type guard

  4. 4

    Narrow return type based on input using generics

  5. 5

    function to return something based on input using R

  6. 6

    Calling angularjs function on text input based on length

  7. 7

    Arbitrary Function - Generate return type according to input

  8. 8

    C++ Function Return/Input Reference Type

  9. 9

    Casting Function Return Type Based on Class Field

  10. 10

    Generic function where the return type depends on the input type in Scala?

  11. 11

    Typescript: Constrain function generic type based on the expected return type

  12. 12

    c++ return type of function to be a function of it's input

  13. 13

    C++ template metaprogramming - return a type based on runtime input

  14. 14

    C++ Return Type Deduction Based on Input Parameters

  15. 15

    How do I set input type based on variable in angularjs?

  16. 16

    How is HTML restricting text input based on the return value of a javascript function?

  17. 17

    Function return type mismatch phenomenon when returning different input ranges

  18. 18

    How to derive a column name in the return type from input parameters to the function?

  19. 19

    Function return type mismatch phenomenon when returning different input ranges

  20. 20

    How to derive a column name in the return type from input parameters to the function?

  21. 21

    How to infer return type of Promise based on function arguments?

  22. 22

    Python Typing: declare return value type based on function argument

  23. 23

    Kotlin: unchecked type inference based on function return value

  24. 24

    Typescript: Wrong overload selection based on function return type

  25. 25

    Input type number in AngularJS

  26. 26

    How can I type-hint a function where the return type depends on the input type of an argument?

  27. 27

    Is it possible to define a function return type based on a defined mapping from the type of a function argument?

  28. 28

    How can I write a function have a polymorphic return type based on the type argument of its type parameter?

  29. 29

    Call function based on input

HotTag

Archive