3

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.

3
  • I would create custom directive that works with numbers but doesn't change ngModel Commented Jun 1, 2017 at 11:10
  • You could cast the string to a number if and when it is for a number type input Commented Jun 1, 2017 at 11:13
  • Is this AngularJS or Angular? I mean, revise the naming and tags in your question accordingly based on what framework you are using :) Commented Jun 1, 2017 at 11:58

1 Answer 1

4

Do you can transform string to number:

$scope.age = parseInt(ageString);

At the HTML:

<input type="number" name="age" ng-model="age"/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.