San Jay Falcon
San Jay Falcon

Reputation: 1013

How to access form validation properties in the controller

As seen here: https://docs.angularjs.org/api/ng/input/input%5Bemail%5D

for example, how to access the template var:

{{myForm.input.$valid}}

in the controller?

$scope.myForm.input.$valid

doesnt do it

Upvotes: 0

Views: 428

Answers (1)

Ku Jin Shin
Ku Jin Shin

Reputation: 117

You can add $watch to $scope.myForm.input.$valid. Here is working example.

<script>
angular.module('test', [])
  .controller('formController', ['$scope', function($scope) {
    $scope.myForm = {};
    $scope.$watch('myForm.input.$valid', function(newVal) {           
        $scope.valid = newVal;
    });

  }]);
</script>

<form name="myForm" ng-controller="formController">
  Email: <input type="email" name="input" ng-model="text" required>
  {{ myForm.input.$valid  }}
  {{ valid  }}
</form>

Upvotes: 1

Related Questions