How to detect if an object $ error of AngularJs forms is empty?

0

I need to deactivate a botton with ng-disabled if there is any error in a form.

form(name="myForm", action="", method="post", novalidate)

input(type="submit", value="Register" ng-disabled="//I need to get true here")

I have tried things like ng-disabled=myform.$error but the boton is disabled even if there are no errors in the form , do you have any idea how I can achieve that?

    
asked by Cesar Jr Rodriguez 29.10.2016 в 01:43
source

1 answer

1

Using the Invalid class, using as you mentioned ngDisabled , For example:

var valida = angular.module('validacion', []);

  valida.controller('main', function($scope) {
    
    $scope.submitForm = function() {

      if ($scope.userForm.$valid) {
        alert('Formulario Correcto');
      }

    };

  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script>
<div ng-app="validacion" ng-controller="main">

    <form name="userForm" ng-submit="submitForm()" novalidate>
        <div class="form-group" ng-class="{ 'has-error' : userForm.usuario.$invalid && !userForm.usuario.$pristine }">
            <label>Usuario</label>
            <input type="text" name="usuario" class="form-control" ng-model="user.usuario" ng-minlength="3" required>
            <span ng-show="userForm.usuario.$error.required"></span>
            <p ng-show="userForm.usuario.$error.minlength" class="help-block">Nombre de Usuario muy Corto.</p>
        </div>
        
        <button type="submit" class="btn btn-primary" ng-disabled="userForm.$invalid">Enviar</button>
        
    </form>
</div>
    
answered by 29.10.2016 в 02:07