AngularJS 'controller as' and form.$valid


I want to check if a form is valid inside an angular controller. This seems straightforward when using $scope, but I can’t get it to work with the ‘controller as’ syntax.

When I attempt to access form.$valid I get the error message “Cannot read property ‘$valid’ of undefined”.



<div ng-app="demoControllerAs" ng-controller="MainController as main">
  <form name="contactForm" novalidate>
      <input type="email" name="email" ng-model="" required />
      <textarea name="message" ng-model="main.message" required></textarea>
    <input type="submit" value="submit" ng-click="main.submit()" />


var app = angular.module('demoControllerAs', []);

app.controller('MainController', [function () {
    var main = this;

    main.submit = function () {
        var isValid = main.contactForm.$valid;


You could do as @ons-jjss suggested but if you prefer not to inject $scope into you controller, then just change your form name as

<form name="main.contactForm" novalidate>

and it’ll work like a charm.

Answered By – Mikko Viitala

Answer Checked By – Clifford M. (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.