1

In a web app (AngualrJS 1.5 + HTML5) I have an input type="number". I want to step in 0.5. The problem is that if the user write 0.7, the form doesn't notificate the error. How can I do to automatic round the number in the field?

This is the html code:

 <input type="number" ng-model="height" name="height" min="1" max="10000" step="0.5">

I've tried to add this js code when I save the page:

$scope.age = (Math.round($scope.age * 2) / 2).toFixed(1);

but I get this error in console:

angular.js?v=0.2:13708 Error: [ngModel:numfmt] Expected `6.0` to be a number

UPDATE: I've made a plunker: PLNKR

3
  • 1
    jsFiddle or something like that? Commented Sep 21, 2016 at 10:55
  • 1
    Looks like you are assigning String to model,but it should be number.Try parseFloat(yourmodel) Commented Sep 21, 2016 at 11:00
  • I've made a plunker Commented Sep 21, 2016 at 11:02

1 Answer 1

1

Have a look into below code.

<body ng-app="numberExample">
  <script>
  angular.module('numberExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.age = 5;
      $scope.roundNo = function(){
        $scope.age = (Math.round($scope.age * 2) / 2);
      };
      $scope.save = function(){
      }
    }]);
</script>
<form name="myForm" ng-controller="ExampleController">

    <input type="number" name="input" ng-model="age"
           min="0" max="1000" step="0.5" ng-change="roundNo()" required>

 </form>
 <button ng-click="save()">Save</button>
</body>

Working plunker here

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.