Text Input allow only Integer input in angularjs

前端 未结 7 1030
梦如初夏
梦如初夏 2021-01-04 19:45

I want to set the html input[number] to allow only integer input (not float).

Basically, the html input

相关标签:
7条回答
  • 2021-01-04 20:31

    I have looked for this many times, I had used directives in previous projects, but have finally founded a way that angular js provides by itself and hence can get rid of directives. Take a look at this code given at this link angularJs

    The code shown there is as follows :

    <script>
      angular.module('numberExample', [])
        .controller('ExampleController', ['$scope', function($scope) {
          $scope.example = {
            value: 12
          };
        }]);
    </script>
    <form name="myForm" ng-controller="ExampleController">
      <label>Number:
        <input type="number" name="input" ng-model="example.value"
               min="0" max="99" required>
     </label>
      <div role="alert">
        <span class="error" ng-show="myForm.input.$error.required">
          Required!</span>
        <span class="error" ng-show="myForm.input.$error.number">
          Not valid number!</span>
      </div>
      <tt>value = {{example.value}}</tt><br/>
      <tt>myForm.input.$valid = {{myForm.input.$valid}}</tt><br/>
      <tt>myForm.input.$error = {{myForm.input.$error}}</tt><br/>
      <tt>myForm.$valid = {{myForm.$valid}}</tt><br/>
      <tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/>
     </form>
    
    0 讨论(0)
提交回复
热议问题