user5417542
user5417542

Reputation: 3376

AngularJS input type number with string model

I am getting an error message due to an input field I have. It is of type "number" but the actual model is a string. Reason behind this is, that my ngModel value is used in other parts of the application.

However for just one Input Field I need to restrict this as only using numbers.

What is the easiest way to achieve this, without changing the type of ngModel.

Upvotes: 3

Views: 1339

Answers (1)

Diogo Machado
Diogo Machado

Reputation: 429

Do you can transform string to number:

$scope.age = parseInt(ageString);

At the HTML:

<input type="number" name="age" ng-model="age"/>

Upvotes: 4

Related Questions