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!
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.
Comments