ng-maxlength="1" does not work - Angular

1

Good afternoon, I have an input of type "number" and I need to limit the number of characters, but the way I'm doing is not working.

<input id="turbidez" type="number" ng-maxlength="7" ng-model="turbidez.turbidez" required />

When I type the field type "number", ng-maxlength works, the problem is that I can not leave field without type, as it will affect other parts of the system.

Does anyone have any ideas?

    
asked by anonymous 22.08.2017 / 21:45

1 answer

1

A property maxlength determines a maximum length value, useful for content of type string .

A numeric value has a maximum value instead.

Try this:

<input id="turbidez" 
 type="number" 
 max="9999999" // Máximo valor com 7 casas decimais.
 ng-model="turbidez.turbidez" 
 required />

Functional example:

angular.module('myApp', [])
.controller('myController', function($scope){
  $scope.turbidez = 5000;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.js"></script><divng-app="myApp">
  <form name="myForm" ng-controller='myController'>
    
    <input name="turbidez" 
 type="number" 
 max="9999999" // Máximo valor com 7 casas decimais.
 ng-model="turbidez" 
 required />
 
    <div role="alert">
      <span class="error" ng-show="myForm.turbidez.$error.required">
      Valor obrigatório.</span>
      <span class="error" ng-show="myForm.turbidez.$error.number">
      Valor não é um número.</span>
      <span class="error" ng-show="myForm.turbidez.$error.max">
      Valor acima do aceitável.</span>
    </div>

    <br/>
    
    <tt>valor = {{turbidez}}</tt><br/>
    <tt>myForm.turbidez.$valid = {{myForm.turbidez.$valid}}</tt><br/>
    <tt>myForm.turbidez.$error = {{myForm.turbidez.$error}}</tt><br/>
    <tt>myForm.$valid = {{myForm.$valid}}</tt><br/>
    <tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/>

    
  </form>
</div>

Source.

    
22.08.2017 / 22:01